Gau*_*ngh 2 java string split stringtokenizer
我想在java中用它的标记拆分一个字符串.例如;
String s = "A#B^C&D!ased&acdf@Mhj%"
String temp[] = s.split("[#^&!@%]+");
Current output :-
temp[0] = A
temp[1] = B
temp[2] = C
temp[3] = D
temp[4] = ased
output which i want :-
temp[0] = A#
temp[1] = B^
temp[2] = C&
temp[3] = D!
temp[4] = ased&
My current approach of doing is
pos = find the index of the token in string
pos = add the size of the token in pos
charAtPos = getcharfrom string at index pos
token = token + charAtPos
Run Code Online (Sandbox Code Playgroud)
如果你有任何更好的方法来做它建议.我认为在非常大的字符串上方法效率不高.
尝试使用正面的后视,一个不捕获其输入的正则表达式构造:
String s = "A#B^C&D!ased&acdf@Mhj%";
String temp[] = s.split("(?<=[#^&!@%]+)");
Run Code Online (Sandbox Code Playgroud)
该(?<=expr)结构在之后的点相匹配expr,没有捕获expr本身,让你在后面的隔板位置拆分文本.