Java split usage Summary

Source: Internet
Author: User

The difference between Split ("") and split.

  • There is a space between Split ("") and ""

String S = "The rain in Spain falls mainly in the plain."; // "there are spaces between them, and each space character is decomposed. String [] Ss = S. split (""); system. out. println ("[ss [0] =" + SS [0] + "]"); system. out. println ("["); For (string: SS) {system. out. println (string);} system. out. println ("]");

Program effect:
[Ss [0] = The]
[
The
Rain
In
Spain
Falls
Mainly
In
The
Plain.
]

========================================================== ==========

  • There is no space between Split ("") and ""
String S = "The rain in Spain falls mainly in the plain."; // "there is no space between them. String [] Ss = S. [do not translate, just delete the space! System. out. println ("[ss [0] =" + SS [0] + "]"); system. out. println ("["); For (string: SS) {system. out. println (string);} system. out. println ("]");

Program effect:

[ss[0] =][The rain in Spain falls mainly in the plain.]

This is what I encountered when I split the IP address again today! I have never thoroughly studied it before! Ah .....

Although there are explanations on the internet, I still want to summarize it so that you can make your own things!

  • Split using split:
String IP = "127.0.0.1"; string [] S = IP. split (". "); <----- the death of the live token to the results for (string: S) {system. out. println (string );}
  • Improve the split parameter settings after searching for materials
  • Use split ("//.") to split:
     String ip = "127.0.0.1"; //     String [] s = ip.split(".");String [] s = ip.split("//.");for (String string : s) {System.out.println(string);}
Program running effect: 127001
  • Cause Analysis: Split (string RegEx) is a regular expression rather than a normal string.

  • Experience Sharing:
  • 1. The separator is ". "(no output)," | "(cannot get the correct effect) when escaping characters," * "," + "error throws an exception, you must add "//" to the front, such as split (// | );
  • 2. If you use "/" as the separator, you must write it as follows: string. split ("//"), because "//" is used in Java to express "/", the string must be written as follows: string STR = "A // B // C ";
  • Escape Character, which must contain "//";
  • 3. If a string contains multiple separators, you can use "|" as a hyphen, for example, string STR = "Java string-split # test". You can use Str. split ("|-| #") removes each string;

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.