Examples of two methods for splitting strings in Java _java

Source: Internet
Author: User

Objective

I believe you should all know that in Java programming, sometimes we need to divide a string by a particular character, letter, etc., so that we can use part of the string or save all the intercepted content into an array. The following article to share the two methods of segmentation, let's take a look at it.

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

public String[] split(String regex,int limit)

Sample code

public class Stringsplit {public
 static 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]);

  Split up to 3 strings
  int maxsplit = 3;
  Sourcestrarray = Sourcestr.split (",", maxsplit);
  for (int i = 0; i < sourcestrarray.length i++) {
   System.out.println (sourcestrarray[i]);
  }}

Output results:

1
2
3
4
5 1 2
3,4,5

Split's implementation directly calls the Matcher class's split method. When you use a String.split method to separate a string, the delimiter may not get the results we expected if it uses some special characters.

Characters that have special meanings in regular expressions, we must escape when we use them, examples:

public class Stringsplit {public
 static void Main (string[] args) {
  String value = ' 192.168.128.33 ';
  Note to add \, or not out, yeah
  string[] names = Value.split ("\");
  for (int i = 0; i < names.length i++) {
   System.out.println (names[i]);
  }}

Split Separator Summary

1. The character "|", "*", "+" must be added with the escape character, preceded by "\".

2. And if it is "\", then you have to write "\\\\".

3. If there is more than one separator in a string, you can use the "|" As a hyphen.

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

II. Java.util.Tokenizer JDK 1.0 or later

StringTokenizer

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

code example

public class Stringsplit {public
 static 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" split, the returned string array has only 3 elements, for the two delimiter between the empty string will be ignored, this should be used carefully.

But String.split ( String.split is to use regular expression matching, so do not use KMP string matching algorithm) is used in order traversal algorithm, time complexity O (m*n), high, so performance, StringTokenizer much better, for frequent use of string segmentation applications, For example, ETL data processing, the use of StringTokenizer performance can be improved a lot.

Summarize

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.