Java split method

Source: Internet
Author: User

Java split method


1. interval no. "."

(1) str. split (".")

String str = "10.156.35.87"; String [] str1 = str. split ("."); for (int I = 0; I
 
  

The running result is blank, that is, no result is output.


(2) str. split ("\\.")

String str = "10.156.35.87"; String [] str1 = str. split ("\."); for (int I = 0; I
   
    
The running result is as follows:
    

0th: 10 1st: 156 2nd: 35 3rd: 87

2. Single vertical number "|"

(1) str. split ("| ")

String str = "10 | 156 | 35 | 87"; String [] str1 = str. split ("|"); for (int I = 0; I
     
      
The running result is as follows:
      

0th: 1st: 1 2nd: 0 3rd: | 4th: 1 5th: 5 6th: 6 7th: | 8th: 3 9th: 10th: | 11th: 8 12th: 7

(2) str. split ("\ | ")

String str = "10 | 156 | 35 | 87"; String [] str1 = str. split ("\\|"); for (int I = 0; I
       
        
The running result is as follows:
        

0th: 10 1st: 156 2nd: 35 3rd: 87

3. asterisk (*)

(1) str. split ("*")

String str = "10*156*35*87"; String [] str1 = str. split ("*"); for (int I = 0; I
         
          
The running result is as follows:
          

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0*^at java.util.regex.Pattern.error(Pattern.java:1924)at java.util.regex.Pattern.sequence(Pattern.java:2090)at java.util.regex.Pattern.expr(Pattern.java:1964)at java.util.regex.Pattern.compile(Pattern.java:1665)at java.util.regex.Pattern.
           
            (Pattern.java:1337)at java.util.regex.Pattern.compile(Pattern.java:1022)at java.lang.String.split(String.java:2361)at java.lang.String.split(String.java:2403)at com.you.model.JavaSplit.main(JavaSplit.java:29)
           

(2) str. split ("\\*")

String str = "10*156*35*87"; String [] str1 = str. split ("\ *"); for (int I = 0; I
           
            
The running result is as follows:
            

0th: 10 1st: 156 2nd: 35 3rd: 87

4. plus sign "+"

(1) str. split ("+ ")

String str = "10 + 156 + 35 + 87"; String [] str1 = str. split ("+"); for (int I = 0; I
             
              
The running result is as follows:
              

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0+^at java.util.regex.Pattern.error(Pattern.java:1924)at java.util.regex.Pattern.sequence(Pattern.java:2090)at java.util.regex.Pattern.expr(Pattern.java:1964)at java.util.regex.Pattern.compile(Pattern.java:1665)at java.util.regex.Pattern.
               
                (Pattern.java:1337)at java.util.regex.Pattern.compile(Pattern.java:1022)at java.lang.String.split(String.java:2361)at java.lang.String.split(String.java:2403)at com.you.model.JavaSplit.main(JavaSplit.java:29)
               

(2) str. split ("\ + ")

String str = "10 + 156 + 35 + 87"; String [] str1 = str. split ("\ +"); for (int I = 0; I
               
                
The running result is as follows:
                

0th: 10 1st: 156 2nd: 35 3rd: 87

Related Article

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.