Java string to array token

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

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 !!

Источник

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.

Читайте также:  Java core вопросы собеседования

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 .

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.

Читайте также:  Генерация jwt токена python

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.

Источник

Java: Example — String to «tokens»

It’s common to have to separate a string into separate «tokens». These tokens can be words, numbers, commands, or whatever. There are several ways to do this, but all of the good solutions use regular expressions.

  • String split(regex) — Probably the easiest.
  • java.util.Scanner — This can «read» from strings. Very general.
  • The java.util.regex. Pattern and Matcher use regular expressions — The most powerful solution.
  • java.util.StringTokenizer — This has been superceded by regular expressions, Scanner, and split().

Easy — String split(. ) method

The easiest way to split a string into separate «tokens» is to use the split(. ) method. For example,

String test = "It's the number 1 way."; String[] tokens = test.split(" "); // Single blank is the separator. System.out.println(Arrays.toString(tokens));

Produces the following output:

Good — Scanner

Scanner can be used to «read» strings. Here’s the previous example using Scanner, but because it doesn’t produce arrays, the results are added to an ArrayList.

String test = "It's the number 1 way."; ArrayList tokens = new ArrayList(); Scanner tokenize = new Scanner(test); while (tokenize.hasNext()) < tokens.add(tokenize.next()); >System.out.println(tokens);

Produces the same output as above:

Читайте также:  Php если строка длинная

It doesn’t care about what makes up a «token», only that they must be separated by single blanks. To allow one or more blanks as a separator, use » +», which means one or more blanks.

Scanner has numerous methods for working more generally with regular expressions to identify the token you want to read or the delimiters you want to skip.

Numbers. One advantage of using a Scanner is that you can easily switch back and forth between reading strings and numbers.

Full regular expression processing using Pattern and Matcher

Old — java.util.StringTokenizer

I’ve left this in here because it’s already written.

//-------------------------------------------------- stringToArray() // Put all "words" in a string into an array. String[] stringToArray(String wordString) < String[] result; int i = 0; // index into the next empty array element //--- Declare and create a StringTokenizer StringTokenizer st = new StringTokenizer(wordString); //--- Create an array which will hold all the tokens. result = new String[st.countTokens()]; //--- Loop, getting each of the tokens while (st.hasMoreTokens()) < result[i++] = st.nextToken(); >return result; >

Источник

Programming for beginners

Write a function that takes a string and delimiter as input and return the array as output. Output array contain the tokens. Function should be flexible enough, where user can choose the option to trim the tokens and ignore empty tokens.

public static String[] tokenize(final String str, final String delimiter, final boolean trimTokens, final boolean ignoreEmptyTokens)

Find the below working application.

StringTokenizerDemo.java

package com.sample.app.strings; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; public class StringTokenizerDemo < /** * Tokenize the given String into a String array. * * @param str input string * @param delimiter * @param trimTokens trim the tokens if set to true. * @param ignoreEmptyTokens Ignore empty tokens, if set to true. * @return null, if the input string is null, else tokenize the given string * with given delimiter and return the tokens as an array. */ public static String[] tokenize(final String str, final String delimiter, final boolean trimTokens, final boolean ignoreEmptyTokens) < if (str == null) < return null; > if (delimiter == null || delimiter.isEmpty()) < return new String[] < str >; > final StringTokenizer stringTokenizer = new StringTokenizer(str, delimiter); final List tokens = new ArrayList<>(); while (stringTokenizer.hasMoreTokens()) < String token = stringTokenizer.nextToken(); if (trimTokens) < token = token.trim(); >if (ignoreEmptyTokens && token.length() == 0) < continue; > tokens.add(token); > return tokens.toArray(new String[0]); > public static void main(String[] args) < final String message = "Hi there, ,how are you"; final String[] tokens1 = tokenize(message, ",", true, true); final String[] tokens2 = tokenize(message, ",", false, true); final String[] tokens3 = tokenize(message, ",", true, false); System.out.println(Arrays.asList(tokens1)); System.out.println(Arrays.asList(tokens2)); System.out.println(Arrays.asList(tokens3)); > >
[Hi there, how are you] [Hi there, , how are you] [Hi there, , how are you]

Источник

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