String s1="Hello World"可以通过s1.split(" ")转换为数组{"Hello","World"}
String s2="System.out.println(s1)"转换为数组{"System","out","println","s1"}
String s2="System.out.println(s1)"转换为数组{"System","out","println","s1"}
调试欢乐多
public static void main(String[] args) throws Exception {
String str = "System.out.println(s1)";
String[] strs = str.split("[.()]");
for(String s : strs) {
System.out.println(s);
}
}
}
char[] CHS = { '+', '-', '/', '=', '(', ')', '[', ']', '{', '}', '<',
'>', ':', '?', ';', ',', '.', '!', '|', '&', '%', '\'', '"',
' ', '\n', '\r', '\t' }; // 符号数组
String regexs = "[" + new String(CHS) + "]";
String[] arr = source.split(regexs);
但是一直提示错误:
java.util.regex.PatternSyntaxException: Unclosed character class near index 28
[+-/=()[]{}<>:?;,.!|&%'" ]
^
28好像是最后的那个],这是怎么回事,而且在CHS数组里面加入乘号*也会报同样的错误
String s2="System.out.println(s1)";
String[] s = s2.split("[.()]");
for(String s1:s) {
System.out.println(s1);
}