简体   繁体   中英

String Tokenizing in java

I need to tokenize a string using a delimiter.

StringTokenizer is capable of tokenizing the string with given delimiter. But, when there are two consecutive delimiters in the string, then it is not considering it as a token.

Thanks in advance for you help

Regards,

The second parameter to the constructor of StringTokenizer object is just a string containing all delimiters that you require.

StringTokenizer st = new StringTokenizer(str, "@!");

In this case, there are two delimiters both @ and !

Consider this example :

String s = "Hello, i am using Stack Overflow;";
System.out.println("s = " + s);
String delims = " ,;";
StringTokenizer tokens = new StringTokenizer(s, delims);
while(tokens.hasMoreTokens())
  System.out.println(tokens.nextToken());

Here you would get an output similar to this with 3 delimiters :

Hello
,

i

am

using

Stack

Overflow

;

Look into String.split()

This should do what you are looking for.

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Use the split() method of java.lang.String and pass it a regular expression which matches your one or more delimiter condition.

for eg "a||b|||c||||d" could be tokenised with split("\\\\|{2,}"); with the resulting array [a,b,c,d]

public StringTokenizer(String str,
                       String delim)

Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.
So for that you can use ..

String.split(String delim);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM