string – How does the java stringtokenizer.nextToken(delimiter); work?

string – How does the java stringtokenizer.nextToken(delimiter); work?

By Default the Delimiters is space , if you do not supply one

    // Extracted StringTokenizer.java
     public StringTokenizer(String string) 
    {
            this(string,  tnrf, false); 
    }

If you supply = as delimiter along with the string then it splits

StringTokenizer st = new StringTokenizer(Phone Number = 790-3233,=);

Following the remark from the API docs:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Id use split in this case:

String text = Phone Number = 790-3233;
String[] tokens = text.split(\s*=\s*);

The regex s*=s* matches zero or more space-chars, followed by an = sign, followed by zero or more space-chars.

string – How does the java stringtokenizer.nextToken(delimiter); work?

Use :

String[] result = your string=790-3233.split(=);

For string Tokenizer, see reference.

Leave a Reply

Your email address will not be published. Required fields are marked *