Monday 13 March 2023

String Tokenizer in java

 In Java, the StringTokenizer class is used to break a string into tokens (substrings) based on a specified delimiter. It is part of the java.util package and is widely used for parsing text data.

Here's an example of how to use StringTokenizer:

String Tokenizer Example program in java

import java.util.StringTokenizer;

public class StringTokenizerExample {
    public static void main(String[] args) {
        String input = "Hello, world! This is a test.";
        StringTokenizer tokenizer = new StringTokenizer(input, ",.! ");

        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            System.out.println(token);
        }
    }
}

In this example, the input string is "Hello, world! This is a test." The StringTokenizer object is created with the input string and the delimiters are specified as ,.! (comma, exclamation mark, period, and space). The hasMoreTokens() method is used to check if there are more tokens to be extracted, and the nextToken() method is used to extract the next token. The extracted token is then printed to the console.

The output of this program will be:

Hello
world
This
is
a
test
 

In this example, the delimiters are specified as a string of characters. However, it is also possible to specify the delimiters as individual characters using the StringTokenizer constructor that takes a second argument of type String or char.

It is important to note that StringTokenizer is a legacy class and has been largely replaced by the split() method of the String class introduced in Java 1.4. The split() method provides a more flexible way of splitting strings using regular expressions. However, StringTokenizer is still useful in situations where a simple delimiter-based approach is sufficient.

String split() Method in java

In Java, the split() method is used to split a string into substrings based on a specified delimiter. It is a method of the String class and is used extensively in text processing and data parsing.

Here's an example of how to use the split() method:

public class SplitExample {
    public static void main(String[] args) {
        String input = "Hello, world! This is a test.";
        String[] tokens = input.split("[,!. ]+");

        for (String token : tokens) {
            System.out.println(token);
        }
    }
}

In this example, the input string is "Hello, world! This is a test." The split() method is called with a regular expression pattern as the delimiter. The regular expression pattern [,.! ]+ specifies that the delimiter can be any combination of commas, exclamation marks, periods, and spaces. The + sign indicates that the delimiter can appear one or more times consecutively. The split() method returns an array of substrings (tokens) that are separated by the delimiter.

The output of this program will be:

Hello
world
This
is
a
test
 

It is important to note that the split() method returns an array of substrings, and that the delimiter is not included in the returned substrings. If the delimiter is not found in the input string, the split() method returns an array with a single element that contains the original string.

The split() method is a powerful tool for text processing and data parsing in Java. It provides a flexible way of splitting strings based on a wide range of delimiters and can be used in a variety of applications.

 split() method in java using regular expressions

In Java, the split() method can be used to split a string into substrings based on a regular expression pattern. Regular expressions are a powerful tool for pattern matching and can be used to specify complex patterns for splitting strings.

Here's an example of how to use the split() method with a regular expression:

public class SplitRegexExample {
    public static void main(String[] args) {
        String input = "John,Paul,George,Ringo";
        String[] tokens = input.split("[,]");

        for (String token : tokens) {
            System.out.println(token);
        }
    }
}



In this example, the input string is "John,Paul,George,Ringo". The split() method is called with the regular expression pattern [,], which specifies that the delimiter is a comma. The split() method returns an array of substrings that are separated by commas.

The output of this program will be:

 John
Paul
George
Ringo

Regular expressions can be used to specify complex patterns for splitting strings. For example, the following regular expression can be used to split a string based on any combination of commas, semicolons, and spaces:

String[] tokens = input.split("[,;\\s]+");
 

In this regular expression, the [] characters are used to specify a character class, which matches any one of the characters inside the class. The + sign indicates that the character class can appear one or more times consecutively. The \\s character class matches any whitespace character, including spaces, tabs, and newlines.

Regular expressions can be a bit tricky to use at first, but they are a powerful tool for text processing and data parsing in Java. They can be used to specify complex patterns for splitting strings, and can be customized to match almost any pattern you can imagine.

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top