‘1-5,11-18周,单周 ’这样的字符串 如何能方便的判断 某个数字 比如6 是否包含在其中。

‘1-5,11-18周,单周 ’这样的字符串 怎么能方便的判断 某个数字 比如6 是否包含在其中。。
‘1-5,11-18周,单周 ’这样的字符串 怎么能方便的判断 某个数字 比如6 是否包含在其中。。
------解决思路----------------------
boolean java.lang.String.contains(CharSequence s)



contains
public boolean contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.
Parameters:
s - the sequence to search for 
Returns:
true if this string contains s, false otherwise 
Throws: 
NullPointerException - if s is null
Since: 
1.5
------解决思路----------------------
引用:
String s = "1-5,11-18周,单周";
String regex = "(\\d+)-(\\d+)";
String num = "6";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
boolean flag = false;
while (matcher.find()) {
String group = matcher.group();
if (Pattern.compile("[" + group + "]").matcher(num).matches()) {
flag = true;
break;
}
}
System.out.println(flag ? "包含" : "不包含");

不要看这个,我脑残了……
下边这个应该没问题:
String s = "1-5,11-18周,单周";
String regex = "(\\d+)-(\\d+)";
int num = 6;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
boolean flag = false;
while (matcher.find()) {
String group = matcher.group();
String[] str = group.split("-");
int a = Integer.parseInt(str[0]);
int b = Integer.parseInt(str[1]);
if (num >= a && num <= b) {
flag = true;
break;
}
}
System.out.println(flag ? "包含" : "不包含");