我用来分割任意数量的空格的字符串的代码是
String out="SELECT * FROM EMP WHERE EMPID=10"; String array[] = out.split("\\s+");
我希望将"out"字符串中的空格包含到array []对象中,同时使用空格分割.我想要的输出是:
array[]={"SELECT","[WHITE SPACE]","*","[WHITE SPACE]","FROM","[WHITE SPACE]","EMP","[WHITE SPACE]","WHERE","[WHITE SPACE]","EMPID=10"}
但我得到的输出是:
array[]={"SELECT","*","FROM","EMP","WHERE","EMPID=10"}
任何帮助将不胜感激
由于split
接受正则表达式,您可以使用外观:
out.split("(?<=\\s)|(?=\\s)");
这将输出:
[SELECT, , *, , FROM, , EMP, , WHERE, , EMPID=10]
请注意,分隔符(在您的情况下为空格)包含在结果中.