Java string tokens to array

Different Ways to Split a String in Java

Learn to split or tokenize a string into an array. Splitting a string is a very common task, especially working on web applications when we have to pass data in CSV format or separate based on some other separator such $ , # or another separator.

The String.split() method is the best and recommended way to split the strings. The tokens are returned in form of a string array that frees us to use it as we wish.

The following Java program splits a string with the delimiter comma. It is equivalent to splitting a CSV file.

String blogName = "how,to,do,in,java"; String[] tokenArray = blogName.split(","); //["how", "to", "do", "in", "java"]

We need to modify the regex expression for any additional requirements. Such for ignoring the whitespaces around commas, we can use the pattern “\\s,\\s”.

String[] tokenArray = blogName.split("\\s*,\\s*");

The String.split() is very straightforward simple API for simple usages. If we want to process the tokens post splitting but before concluding the final result, the Splitter class is the best.

  • Using Splitter makes the code more readable and reusable also. We create a Splitter instance and reuse it multiple times, thus helping achieve uniform logic splitting in the whole application.
  • Another benefit is that it also provided some useful methods while building the splitter itself, eliminating a lot of after-work after creating the tokens.

We can directly include Guava from the maven repository.

We can create a Splitter instance in a fluent manner:

Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults();

And now, use it anywhere in the code as we like. Note that we have the commas twice. The Splitter handles it well and does not include the empty token.

Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults(); Iterable tokensList = niceCommaSplitter.split("how,to,do,in, ,java"); tokensList.forEach(System.out::println); //"how", "to", "do", "in", "java"

The Apache Commons Lang’s StringUtils class provides many useful methods to perform common operations on Strings, such as search, replace, reverse or check empty. All operations are null safe.

The StringUtils.split() is very similar to the above approach and also returns the String array output. The only benefit is that the code is faster.

Start with including the latest version of common-lang3 dependency.

 org.apache.commons commons-lang3 3.12.0  

The constructor to initialize the StringUtils takes the following parameters:

split(String str, String separatorChars, int max)
  • str – the String to parse, may be null.
  • separatorChars (Optional) – the characters used as the delimiters. The default value is whitespace.
  • max (Optional) – the maximum number of elements to include in the array. A zero or negative value implies no limit.

The following Java program using StringUtils splits a string by delimiter whitespace.

String[] tokens = StringUtils.split("how to do in java"); Assertions.assertArrayEquals(new String[], tokens);

Источник

Java – Converting StringTokenizer tokens into ArrayList

1. Converting StringTokenizer tokens into ArrayList

StringTokenizer st = new StringTokenizer(str, “ ”);
  • First define a sample string
  • Create StringTokenizer object and pass sample string as 1 st constructor-argument with space (” “) as 2 nd constructor-argument
  • Create ArrayList object
  • Now, iterate through tokens and simultaneously add tokens iterated into List
Читайте также:  Rblx lanb home php

ConvertTokensIntoArrayList.java

package in.bench.resources.java.stringtokenizer.example; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class ConvertTokensIntoArrayList < public static void main(String[] args) < // sample string String str = "Water Wind Earth Sky Fire"; // create StringTokenizer object StringTokenizer st = new StringTokenizer(str, " "); // create ArrayList object Listelements = new ArrayList(); // iterate through StringTokenizer tokens while(st.hasMoreTokens()) < // add tokens to AL elements.add(st.nextToken()); >// original String System.out.println("Original String : \n" + str); // iterate through AL - using forEach loop System.out.println("\nPrinting elements in ArrayList . "); for(String element : elements) < System.out.println(element); >> >
Original String : Water Wind Earth Sky Fire Printing elements in ArrayList . Water Wind Earth Sky Fire

Alternative approach is to use split() method of String class as StringTokenizer is deprecated

2. Splitting String into Arrays and then converting into ArrayList

String[] strArray = testStr.split(" "); List ls = new ArrayList(Arrays.asList(strArray));
  • First define a sample string
  • Then split sample string using space (“ ”) as delimiter
  • Create new ArrayList object and add string array values using constructor-argument as shown in the syntax

ConvertStringArrayIntoArrayList.java

package in.bench.resources.java.stringtokenizer.example; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ConvertStringArrayIntoArrayList < public static void main(String[] args) < // sample string String str = "Fire Earth Sky Water Wind"; // split string using space as delimiter String[] strArray = str.split(" "); // create ArrayList object Listelements = new ArrayList(Arrays.asList(strArray)); // original String System.out.println("Original String : \n" + str); // iterate through AL - using forEach loop System.out.println("\nPrinting elements in ArrayList . "); for(String element : elements) < System.out.println(element); >> >
Original String : Fire Earth Sky Water Wind Printing elements in ArrayList . Fire Earth Sky Water Wind

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

References:

Happy Coding !!
Happy Learning !!

Источник

Different Ways to Split a String in Java

Learn to split or tokenize a string into an array. Splitting a string is a very common task, especially working on web applications when we have to pass data in CSV format or separate based on some other separator such $ , # or another separator.

The String.split() method is the best and recommended way to split the strings. The tokens are returned in form of a string array that frees us to use it as we wish.

The following Java program splits a string with the delimiter comma. It is equivalent to splitting a CSV file.

String blogName = "how,to,do,in,java"; String[] tokenArray = blogName.split(","); //["how", "to", "do", "in", "java"]

We need to modify the regex expression for any additional requirements. Such for ignoring the whitespaces around commas, we can use the pattern “\\s,\\s”.

String[] tokenArray = blogName.split("\\s*,\\s*");

The String.split() is very straightforward simple API for simple usages. If we want to process the tokens post splitting but before concluding the final result, the Splitter class is the best.

  • Using Splitter makes the code more readable and reusable also. We create a Splitter instance and reuse it multiple times, thus helping achieve uniform logic splitting in the whole application.
  • Another benefit is that it also provided some useful methods while building the splitter itself, eliminating a lot of after-work after creating the tokens.
Читайте также:  Php регулярные выражения только буквы цифры

We can directly include Guava from the maven repository.

We can create a Splitter instance in a fluent manner:

Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults();

And now, use it anywhere in the code as we like. Note that we have the commas twice. The Splitter handles it well and does not include the empty token.

Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults(); Iterable tokensList = niceCommaSplitter.split("how,to,do,in, ,java"); tokensList.forEach(System.out::println); //"how", "to", "do", "in", "java"

The Apache Commons Lang’s StringUtils class provides many useful methods to perform common operations on Strings, such as search, replace, reverse or check empty. All operations are null safe.

The StringUtils.split() is very similar to the above approach and also returns the String array output. The only benefit is that the code is faster.

Start with including the latest version of common-lang3 dependency.

 org.apache.commons commons-lang3 3.12.0  

The constructor to initialize the StringUtils takes the following parameters:

split(String str, String separatorChars, int max)
  • str – the String to parse, may be null.
  • separatorChars (Optional) – the characters used as the delimiters. The default value is whitespace.
  • max (Optional) – the maximum number of elements to include in the array. A zero or negative value implies no limit.

The following Java program using StringUtils splits a string by delimiter whitespace.

String[] tokens = StringUtils.split("how to do in java"); Assertions.assertArrayEquals(new String[], tokens);

Источник

Convert String to Array in Java

Convert String to Array in java

When developing applications in Java there are many cases where we will find ourselves converting data from one type to the other.

Developers must understand how this is done because it will enhance their system effectiveness leading to ease of use from different users interacting with the system.

How to convert String to Array in Java

In this tutorial, we will learn how to convert String to array in Java and an example use case in an application is when we want to break the input search into several sequences and return results based on a sequence that matches a record from our data source.

A String is a data type that is implemented by a sequence of characters in double-quotes such as «Hello! World» while an array is a data structure that allows us to insert, traverse, delete, and search elements of a particular data type.

Using toArray() method of Set

A Set is a collection that only supports unique elements and it provides us with a method that we can leverage to convert a string to an array.

This method is called toArray() and is a generic method which means it can also be applied to other data types in Java.

To achieve this we will create a new array of size 0 and pass it to this method. The collection of elements in the Set will be added to this array and the method will return an array containing our elements.

Note that if the array created is less than the size of the collection the method creates a new array with the same size as the collection to accommodate all the values and returns the new array.

If the size of the array is large than the size of the collection the remaining storage locations are set to null .

Читайте также:  Java как очистить stringbuilder

The type of array returned by this method is of the same type as that of the specified array and the method throws an ArrayStoreException if the specified array type is not a supertype of every element in the collection.

The method also throws a NullPointerException if the specified array is null.

Using the split() method of String class

The split() method of the Stringg class uses a regular expression to split the string by every string that matches the specified regular expression.

The splitting is done by terminating a string with another substring that matches the given regular expression.

The method returns an array consisting of the terminated substrings and the elements are arranged in the order they appear in the original string.

If the regular expression does not match any substring the resulting array has only one element which is this string.

If the regular expressions syntax cannot be recognized the method throws a PatternSyntaxException .

Further reading:

Declare String array in java
How to convert String to Char Array in java

Using StringTokenizor class

From the name of the class, we can conclude that this class provides the functionality to break a string into tokens.

Tokens is a term mostly in programming to refer to a single element such as an identifier, keyword, constants, operators, and separators.

Note that the StringTokenizer cannot differentiate between identifiers, numbers, quoted strings, or how to jump comments but uses delimiters to break the string into tokens.

We will use this functionality to convert our string to an array by fetching each token obtained from our string and adding it to a new array.

Since we want to break our string using a whitespace delimiter, we will pass string whitespace in the second parameter of the StringTokenizer .

The size of our array will be managed by the countTokens() method of the StringTokenizer that returns an int value indicating the number of tokens that are remaining in the string.

We will use a while loop that will consume the hasMoreTokens() method checks if there are more tokens in the string and returns true if there is at least one element and false otherwise.

When the condition evaluates to true we use the nextToken() method to get the next element and add it to our new array.

A counter starting from 0 is used to increment the loop and we also use the counter value as the index of the element in our array.

Note that the use of StringTokenizer is discouraged in new code and it is only maintained for compatibility reasons because it is a legacy class.

The split() method which is discussed in this tutorial, should be used as an alternative to achieving similar functionality.

Источник

Оцените статью