Split string in Java

Source: Internet
Author: User

java.lang.String 's split () method , JDK 1.4 or later

Public string[] Split (String regex,int limit)

Sample code

PublicClassStringsplit {Publicstatic void main ( String[] args) {String sourcestr = "1,2,3,4,5" ; string[] Sourcestrarray = Sourcestr.split ("," for (int i = 0; i < sourcestrarray.length; I++) {System.out.println (sourcestrarray[i]);} // up to 3 strings divided int Maxsplit = 3for (int i = 0; i < sourcestrarray.length; I++          

Output Result:

12345123,4,5

Split's implementation directly calls the split method of the Matcher class. When you use the String.Split method to separate strings, separators may not get the results we expect if they use some special characters. Characters that have special meanings in regular expressions must be escaped when we use them, for example:

Class Stringsplit {    void main (string[] args) {String value = "192.168.128.33"// note to add \ \ or not to come out, Yeah string[] names = value.split ("\ \.") For (int i = 0; i < names.length; i++) {System.out.println (names[i]);    }}} 

Split Delimiter Summary

1. The character "|", "*", "+" are added with the escape character, preceded by "\ \".
2. And if "\", then you have to write "\\\\".
3. If there is more than one delimiter in a string, you can use the ' | ' As a hyphen.

For example: string str = "Java string-split#test", you can use Str.split ("|-|#") to separate each string. This divides the string into 3 substrings.

Java.util.Tokenizer JDK 1.0 or later

StringTokenizer

The StringTokenizer class allows an application to decompose a string into tokens. StringTokenizer is a legacy class that is retained for compatibility reasons (although it is not encouraged in new code). It is recommended that all people seeking this function use the split method of String or the Java.util.regex package.

code example

Class Stringsplit {    void main (string[] args) {String IP = "192.168.128.33"; StringTokenizer token=New StringTokenizer (IP, ".") while (token.hasmoreelements ()) {System.out.print (Token.nexttoken () + "");     }}} 

But StringTokenizer for the string "192.168..33" of the split, the returned string array only 3 elements, for two separators between the empty string will be ignored, this should be used with caution.

But String.Split (String.Split is to use regular expression matching, so do not use KMP string matching algorithm) is a sequential traversal algorithm, time complexity O (m*n), high, so performance, StringTokenizer much better, For applications that use string splitting frequently, such as ETL data processing, the use of StringTokenizer performance can improve a lot.

Split string in Java

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.