How to Read Letters and Numbers in One String Stringtokenizer

StringTokenizer is a legacy form for splitting strings into tokens. In order to break String into tokens, yous need to create a StringTokenizer object and provide a delimiter for splitting strings into tokens. Yous tin pass multiple delimiters e.yard. yous can break String into tokens by, and: at the same time. If you don't provide any delimiter then by default it will use white-space. It'south inferior to dissever() as it doesn't support regular expression, also it is not very efficient. Since information technology's an obsolete class, don't wait any performance improvement either. On the hand split() has gone some major performance heave on Java 7, run into hither to larn more virtually splitting Cord with regular expression.

StringTokenizer looks easier to utilise but yous should avoid information technology, except for piddling task. E'er  Prefer Cord'southward split() method for splitting String and for repeated separate utilise Design.split() method.

Coming dorsum to StringTokenizer, we will meet three examples of StringTokenizer in this commodity. The kickoff example is to pause String based on white-infinite, the second case will prove how to use multiple delimiters, and the third example will show you how to count the number of tokens.

In order to get tokens, you basically follow the Enumeration mode model, i.e. checking for more than tokens using hasMoreTokens() and and then getting tokens using nextToken().

And, If you are new to the Java globe and so I also recommend you become throughThe Complete Coffee MasterClasson Udemy to learn Coffee in a improve and more structured fashion. This is one of the best and up-to-date courses to larn Coffee online.

Java StringTokenizer Instance

StringTokenizer Example in Java with Multiple DelimiterHere is the total lawmaking of our Coffee StringTokenizer Example. You can copy-paste this code into your favorite IDE and run information technology straight away. It doesn't require any third-party library like Apache commons or Google Guava. All you need to do is create a Java source file with the same name as the public class of this example, the IDE will take care of compiling and running this example.

Alternatively, you lot tin also compile and execute this case from the control prompt also. If you lot await at the first example, we accept a String where words are separated by a white-infinite, and to get each give-and-take from that String, nosotros accept created a StringTokenizer object by passing that String itself, notice we have not provided whatsoever delimiter, because by default StringTokenizer uses white-space as a token separator.

In order to become each token, in our example discussion, y'all but need to loop, until hasMoreTokens() returns false. Now to get the discussion itself, simply call nextToken() method of StringTokenizer. This is similar to Iterating over Java Collection using the Iterator, where we use the hasNext() method as while loop condition and next() method to become the next element from Collection.

The 2d example is more interesting because hither our text is a web address, which has protocol and IP address. Hither we are passing multiple delimiters to carve up http string e.g. //( double slash), : (colon) and . (dot), Now StringTokenizer volition create a token if any of this is found in target String.


The third example shows you how to get a total number of tokens from StringTokenizer, quite useful if you want to copy tokens into an assortment or collection, every bit y'all can use this number to decide the length of array or size of the respective collection.

import java.util.StringTokenizer ;

/**

 * Java program to prove how to employ StringTokenizer for breaking a delimited

 * String into tokens. StringTokenizer allows yous to use multiple delimiters as

 * well. which means you tin can split up String containing comma and colon in one call.

 *

 * @writer Javin Paul

 */

public grade StringTokenizerDemo {

public static void main (Cord args[]) {

// Case i - By default StringTokenizer breaks Cord on space

        System. out . println ( "StringTokenizer Example in Java, split String on whitespace" );

        String discussion = "Which one is meliorate, StringTokenizer vs Split up?" ;

        StringTokenizer tokenizer = new StringTokenizer(discussion);

while (tokenizer. hasMoreTokens ()) {

            System. out . println (tokenizer. nextToken ());

        }

// Case 2 - StringTokenizer with multiple delimiter

        System. out . println ( "StringTokenizer multiple delimiter Instance in Java" );

        String msg = "http://192.173.15.36:8084/" ;

        StringTokenizer st = new StringTokenizer(msg, "://." );

while (st. hasMoreTokens ()) {

            System. out . println (st. nextToken ());

        }

// Example 3 - Counting number of String tokens

        System. out . println ( "StringTokenizer count Token Example" );

        String records = "one,2,three,4,five,half-dozen,seven" ;

        StringTokenizer breaker = new StringTokenizer(records, "," );

        Organization. out . println ( "Total number of tokens : " + breaker. countTokens ());

    }

}

Output:

StringTokenizer Instance in Java, split String on whitespace

Which

ane

is

better,

StringTokenizer

vs

Split?

StringTokenizer multiple delimiter Instance in Java

http

192

173

15

36

8084

StringTokenizer count Token Example

Total number of tokens : 7

As I said, all this functionality is also available to the Cord class' carve up method, and you should utilise that as your default tool for creating tokens from String or breaking them based upon whatever limiter. To learn more about thepros and cons of using the StringTokenizer and Split method,  you can see my post difference betwixt Split vs StringTokenizer in Java.

That's all on how to use StringTokenizer in Java with multiple delimiters. Yeah, information technology'southward convenient, specially if you are not very comfortable with regular expression. By the way,  if that'southward the case and then you better spend some fourth dimension learning regular expression, non only to separate Cord into tokens just to use regex equally a skill.

You lot will be surprised to see the ability of regular expression, while searching, replacing and doing other text stuff. StringTokenizer is as well a legacy grade, which is simply retained for compatibility reasons and you should not use information technology in new code.

It is recommended to utilize the split method of Cord for splitting strings into tokens or Patterns.split() method from java.util.regex parcel instead. In terms of performance also, split() has got a major boost in Java 7 from Coffee vi, and it's reasonable to expect performance comeback only on thesplit() method, because no piece of work will be washed on StringTokenizer.

hallenbeckcoully.blogspot.com

Source: https://javarevisited.blogspot.com/2014/02/stringtokenizer-example-in-java-multiple-delimiters.html

0 Response to "How to Read Letters and Numbers in One String Stringtokenizer"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel