Here is a code:
String str="1#2#3";String[] strs=str.split("#");System.out.println(strs.length);OK, the final output result of the above code is 2, which is no problem, but if it is replaced by the following writing method, please see:
String str="##";String[] strs=str.split("#");System.out.println(strs.length);The result output is 0. If you change the writing method, please see:
String str="1##";String[] strs=str.split("#");System.out.println(strs.length);The result output is 1, so OK, what if I only want 2 in either case, I wrote a piece of code for the conversion like this:
String str="##";str=str.replaceAll("#", "v^#v^");String[] strs=str.split("#");for (int i = 0; i < strs.length; i++) { String text=strs[i].replaceAll("v//^", ""); if("".equals(text)){ text="omitted"; }System.out.println(text);}result:
Summarize
OK, the problem is solved perfectly. The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.