如何使用组合特殊字符拆分 java 中的字符串
How can i split string in java using combine special characters
如何使用组合特殊字符拆分 String
?
例如,如果组合特殊字符是 {@}
:
String str = "This is test string1.{@}This is test string2@(#*$ ((@@{}";
StringTokenizer stoken = new StringTokenizer(str, "\{\@\}");
while (stoken.hasMoreElements()) {
System.out.println(stoken.nextElement());
}
我对上述程序的期望是:
This is test string1.
This is test string2@(#*$ ((@@{}
您不能使用具有特殊含义的字符,例如:\^$。 | ? * + ( ) [ { },我认为有 12 个。因此,更改您的字符以将字符串拆分为类似 "/-/"
的内容
执行此操作的代码如下所示:
public class SplitString {
public static void main(String[] args) {
String s = "This is test string1./This is test string2@(#*$ ((@@{}";
String[] fragments = s.split("/");
String fragment1 = fragments[0];
String fragment2 = fragments[1];
System.out.println(fragment1);
System.out.println(fragment2);
}
}
此解决方案基于此线程中的答案:
Whosebug Split String
如何使用组合特殊字符拆分 String
?
例如,如果组合特殊字符是 {@}
:
String str = "This is test string1.{@}This is test string2@(#*$ ((@@{}";
StringTokenizer stoken = new StringTokenizer(str, "\{\@\}");
while (stoken.hasMoreElements()) {
System.out.println(stoken.nextElement());
}
我对上述程序的期望是:
This is test string1.
This is test string2@(#*$ ((@@{}
您不能使用具有特殊含义的字符,例如:\^$。 | ? * + ( ) [ { },我认为有 12 个。因此,更改您的字符以将字符串拆分为类似 "/-/"
的内容执行此操作的代码如下所示:
public class SplitString {
public static void main(String[] args) {
String s = "This is test string1./This is test string2@(#*$ ((@@{}";
String[] fragments = s.split("/");
String fragment1 = fragments[0];
String fragment2 = fragments[1];
System.out.println(fragment1);
System.out.println(fragment2);
}
}
此解决方案基于此线程中的答案: Whosebug Split String