An interesting phenomenon of split-split strings in Java _java

Source: Internet
Author: User

Here's a snippet of code:

String str= "1#2#3";
String[] Strs=str.split ("#");
System.out.println (strs.length);       

OK, the above code final output is 2, this is no problem, but if you change the following wording, please see:

String str= "# #";
String[] Strs=str.split ("#");
System.out.println (strs.length);

The result output is 0, if you change another way of writing, see:

String str= "1##";
String[] Strs=str.split ("#");
System.out.println (strs.length);

The result output is 1, so well, if I just want 2 in any case, I write a code that converts:

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= "slightly";
  }
System.out.println (text);

Results:

Summarize

Well, the problem is solved perfectly. The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.