Em Java, divisão de string é uma operação importante e geralmente usada durante a codificação. Java fornece várias maneiras de dividir a corda . Mas a maneira mais comum é usar o Método split() da classe String. Nesta seção, aprenderemos como dividir uma String em Java com delimitador. Junto com isso, também aprenderemos alguns outros métodos para dividir a string, como o uso da classe StringTokenizer, Método Scanner.useDelimiter() . Antes de passar ao assunto, vamos entender o que é delimitador.
O que é um delimitador?
Em Java , delimitadores são os caracteres que dividem (separam) a string em tokens. Java nos permite definir qualquer caractere como delimitador. Existem muitos métodos de divisão de string fornecidos por Java que usam caracteres de espaço em branco como delimitador. O delimitador de espaço em branco é o delimitador padrão em Java.
Antes de passarmos ao programa, vamos entender o conceito de string.
A string é composta por dois tipos de texto que são fichas e delimitadores. Os tokens são as palavras significativas e os delimitadores são os caracteres que dividem ou separam os tokens. Vamos entender isso através de um exemplo.
Para entender o delimitador em Java , devemos ser amigáveis com o Expressão regular Java . É necessário quando o delimitador é usado como caractere especial nas expressões regulares, como (.) e (|).
Exemplo de delimitador
Corda: Javatpoint é o melhor site para aprender novas tecnologias.
Na string acima, os tokens são, Javatpoint, é o melhor site para aprender novas tecnologias , e os delimitadores são espaços em branco entre os dois tokens.
Como dividir uma string em Java com delimitador?
Java fornece a seguinte maneira de dividir uma string em tokens:
Usando o método Scanner.next()
É o método da classe Scanner. Ele encontra e retorna o próximo token do scanner. Ele divide a string em tokens por delimitador de espaço em branco. O token completo é identificado pela entrada que corresponde ao padrão delimitador.
Sintaxe:
public String next();
Isso joga NoSuchElementException se o próximo token não estiver disponível. Também joga IllegalStateException se o scanner de entrada estiver fechado.
Vamos criar um programa que divide um objeto string usando o método next() que usa espaços em branco para dividir a string em tokens.
SplitStringExample1.java
import java.util.Scanner; public class SplitStringExample1 { public static void main(String[] args) { //declaring a string String str='Javatpoint is the best website to learn new technologies'; //constructor of the Scanner class Scanner sc=new Scanner(str); while (sc.hasNext()) { //invoking next() method that splits the string String tokens=sc.next(); //prints the separated tokens System.out.println(tokens); //closing the scanner sc.close(); } } }
Saída:
matemática redonda java
Javatpoint is the best website to learn new technologies
No programa acima, é importante notar que no construtor da classe Scanner ao invés de passar o System.in passamos uma variável string str. Fizemos isso porque antes de manipular a string, precisamos lê-la.
Usando o método String.split()
O dividir() método do Corda aula é usado para dividir uma string em uma matriz de objetos String com base no delimitador especificado que corresponde à expressão regular. Por exemplo, considere a seguinte sequência:
String str= 'Welcome,to,the,word,of,technology';
A string acima é separada por vírgulas. Podemos dividir a string acima usando a seguinte expressão:
gráfico de alocação de recursos
String[] tokens=s.split(',');
A expressão acima divide a string em tokens quando os tokens são separados pelo caractere delimitador especificado vírgula (,). A string especificada é dividida nos seguintes objetos de string:
Welcome to the word of technology
Existem duas variantes dos métodos split():
- dividir (string regex)
- split(String regex, limite interno)
String.split(String regex)
Ele divide a string de acordo com o regex de expressão regular especificado. Podemos usar ponto (.), espaço ( ), vírgula (,) e qualquer caractere (como z, a, g, l, etc.)
Sintaxe:
public String[] split(String regex)
O método analisa uma expressão regular delimitadora como um argumento. Ele retorna uma matriz de objetos String. Isso joga PatternSyntaxException se a expressão regular analisada tiver uma sintaxe inválida.
Vamos usar o método split() e dividir a string por vírgula.
SplitStringExample2.java
public class SplitStringExample2 { public static void main(String args[]) { //defining a String object String s = 'Life,is,your,creation'; //split string delimited by comma String[] stringarray = s.split(','); //we can use dot, whitespace, any character //iterate over string array for(int i=0; i<stringarray.length; i++) { prints the tokens system.out.println(stringarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> Life is your creation </pre> <p>In the above example, the string object is delimited by a comma. The split() method splits the string when it finds the comma as a delimiter.</p> <p>Let's see another example in which we will use multiple delimiters to split the string.</p> <p> <strong>SplitStringExample3.java</strong> </p> <pre> public class SplitStringExample3 { public static void main(String args[]) { //defining a String object String s = 'If you don't like something, change.it.'; //split string by multiple delimiters String[] stringarray = s.split('[, . ']+'); //iterate over string array for(int i=0; i<stringarray.length; i++) { prints the tokens system.out.println(stringarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> If you don t like something change it </pre> <p> <strong>String.split(String regex, int limit)</strong> </p> <p>It allows us to split string specified by delimiter but into a limited number of tokens. The method accepts two parameters regex (a delimiting regular expression) and limit. The limit parameter is used to control the number of times the pattern is applied that affects the resultant array. It returns an array of String objects computed by splitting the given string according to the limit parameter.</p> <p>There is a slight difference between the variant of the split() methods that it limits the number of tokens returned after invoking the method.</p> <p> <strong>Syntax:</strong> </p> <pre> public String[] split(String regex, int limit) </pre> <p>It throws <strong>PatternSyntaxException</strong> if the parsed regular expression has an invalid syntax.</p> <p>The limit parameter may be positive, negative, or equal to the limit.</p> <p> <strong>SplitStringExample4.java</strong> </p> <pre> public class SplitStringExample4 { public static void main(String args[]) { String str1 = '468-567-7388'; String str2 = 'Life,is,your,creation'; String str3 = 'Hello! how are you?'; String[] stringarray1 = str1.split('8',2); System.out.println('When the limit is positive:'); System.out.println('Number of tokens: '+stringarray1.length); for(int i=0; i<stringarray1.length; i++) { system.out.println(stringarray1[i]); } string[] stringarray2="str2.split('y',-3);" system.out.println(' when the limit is negative: '); system.out.println('number of tokens: '+stringarray2.length); for(int i="0;" i<stringarray2.length; system.out.println(stringarray2[i]); stringarray3="str3.split('!',0);" equal to 0:'); '+stringarray3.length); i<stringarray3.length; system.out.println(stringarray3[i]); < pre> <p> <strong>Output:</strong> </p> <pre> When the limit is positive: Number of tokens: 2 46 -567-7388 When the limit is negative: Number of tokens: 2 Life,is, our,creation When the limit is equal to 0: Number of tokens: 2 Hello how are you? </pre> <p>In the above code snippet, we see that:</p> <ul> <li>When the limit is 2, the number of tokens in the string array is two.</li> <li>When the limit is -3, the specified string is split into 2 tokens. It includes the trailing spaces.</li> <li>When the limit is 0, the specified string is split into 2 tokens. In this case, trailing space is omitted.</li> </ul> <h3>Example of Pipe Delimited String</h3> <p>Splitting a string delimited by pipe (|) is a little bit tricky. Because the pipe is a special character in Java regular expression.</p> <p>Let's create a string delimited by pipe and split it by pipe.</p> <p> <strong>SplitStringExample5.java</strong> </p> <pre> public class SplitStringExample5 { public static void main(String args[]) { //defining a String object String s = 'Life|is|your|creation'; //split string delimited by comma String[] stringarray = s.split('|'); //we can use dot, whitespace, any character //iterate over string array for(int i=0; i<stringarray.length; i++) { prints the tokens system.out.println(stringarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> L i f e | i s | y o u r | c r e a t i o n </pre> <p>In the above example, we see that it does not produce the same output as other delimiter yields. It should produce an array of tokens, <strong>life, yours,</strong> and <strong>creation</strong> , but it is not. It gives the result, as we have seen in the output above.</p> <p>The reason behind it that the regular expression engine interprets the pipe delimiter as a <strong>Logical OR operator</strong> . The regex engine splits the String on empty String.</p> <p>In order to resolve this problem, we must <strong>escape</strong> the pipe character when passed to the split() method. We use the following statement to escape the pipe character:</p> <pre> String[] stringarray = s.split('\|'); </pre> <p>Add a pair of <strong>backslash (\)</strong> before the delimiter to escape the pipe. After doing the changes in the above program, the regex engine interprets the pipe character as a delimiter.</p> <p>Another way to escape the pipe character is to put the pipe character inside a pair of square brackets, as shown below. In the Java regex API, the pair of square brackets act as a character class.</p> <pre> String[] stringarray = s.split('[|]'); </pre> <p>Both the above statements yield the following output:</p> <p> <strong>Output:</strong> </p> <pre> Life is your creation </pre> <h3>Using StringTokenizer Class</h3> <p>Java <strong>StringTokenizer</strong> is a legacy class that is defined in java.util package. It allows us to split the string into tokens. It is not used by the programmer because the split() method of the String class does the same work. So, the programmer prefers the split() method instead of the StringTokenizer class. We use the following two methods of the class:</p> <p> <strong>StringTokenizer.hasMoreTokens()</strong> </p> <p>The method iterates over the string and checks if there are more tokens available in the tokenizer string. It returns true if there is one token is available in the string after the current position, else returns false. It internally calls the <strong>nextToken()</strong> method if it returns true and the nextToken() method returns the token.</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean hasMoreTokens() </pre> <p> <strong>StringTokenizer.nextToken()</strong> </p> <p>It returns the next token from the string tokenizer. It throws <strong>NoSuchElementException</strong> if the tokens are not available in the string tokenizer.</p> <p> <strong>Syntax:</strong> </p> <pre> public String nextToken() </pre> <p>Let's create a program that splits the string using the StringTokenizer class.</p> <p> <strong>SplitStringExample6.java</strong> </p> <pre> import java.util.StringTokenizer; public class SplitStringExample6 { public static void main(String[] args) { //defining a String object String str = 'Welcome/to/Javatpoint'; //constructor of the StringTokenizer class StringTokenizer tokens = new StringTokenizer(str, '/'); //checks if the string has more tokens or not while (tokens.hasMoreTokens()) { //prints the tokens System.out.println(tokens.nextToken()); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Welcome to Javatpoint </pre> <h2>Using Scanner.useDelimiter() Method</h2> <p>Java <strong>Scanner</strong> class provides the <strong>useDelimiter()</strong> method to split the string into tokens. There are two variants of the useDelimiter() method:</p> <ul> <li>useDelimiter(Pattern pattern)</li> <li>useDelimiter(String pattern)</li> </ul> <h3>useDelimiter(Pattern pattern)</h3> <p>The method sets the scanner's delimiting pattern to the specified string. It parses a delimiting pattern as an argument. It returns the Scanner.</p> <p> <strong>Syntax:</strong> </p> <pre> public Scanner useDelimiter(Pattern pattern) </pre> <h3>useDelimiter(String pattern)</h3> <p>The method sets the scanner's delimiting pattern to a pattern that constructs from the specified string. It parses a delimiting pattern as an argument. It returns the Scanner.</p> <p> <strong>Syntax:</strong> </p> <pre> public Scanner useDelimiter(String pattern) </pre> <h4>Note: Both the above methods behave in the same way, as invoke the useDelimiter(Pattern.compile(pattern)).</h4> <p>In the following program, we have used the useDelimiter() method to split the string.</p> <p> <strong>SplitStringExample7.java</strong> </p> <pre> import java.util.Scanner; public class SplitStringExample7 { public static void main(String args[]) { //construtor of the Scanner class Scanner scan = new Scanner('Do/your/work/self'); //Initialize the string delimiter scan.useDelimiter('/'); //checks if the tokenized Strings has next token while(scan.hasNext()) { //prints the next token System.out.println(scan.next()); } //closing the scanner scan.close(); } } </pre> <p> <strong>Output:</strong> </p> <pre> Do your work self </pre> <hr></stringarray.length;></pre></stringarray1.length;></pre></stringarray.length;></pre></stringarray.length;>
No exemplo acima, o objeto string é delimitado por uma vírgula. O método split() divide a string quando encontra a vírgula como delimitador.
Vejamos outro exemplo em que usaremos vários delimitadores para dividir a string.
SplitStringExample3.java
public class SplitStringExample3 { public static void main(String args[]) { //defining a String object String s = 'If you don't like something, change.it.'; //split string by multiple delimiters String[] stringarray = s.split('[, . ']+'); //iterate over string array for(int i=0; i<stringarray.length; i++) { prints the tokens system.out.println(stringarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> If you don t like something change it </pre> <p> <strong>String.split(String regex, int limit)</strong> </p> <p>It allows us to split string specified by delimiter but into a limited number of tokens. The method accepts two parameters regex (a delimiting regular expression) and limit. The limit parameter is used to control the number of times the pattern is applied that affects the resultant array. It returns an array of String objects computed by splitting the given string according to the limit parameter.</p> <p>There is a slight difference between the variant of the split() methods that it limits the number of tokens returned after invoking the method.</p> <p> <strong>Syntax:</strong> </p> <pre> public String[] split(String regex, int limit) </pre> <p>It throws <strong>PatternSyntaxException</strong> if the parsed regular expression has an invalid syntax.</p> <p>The limit parameter may be positive, negative, or equal to the limit.</p> <p> <strong>SplitStringExample4.java</strong> </p> <pre> public class SplitStringExample4 { public static void main(String args[]) { String str1 = '468-567-7388'; String str2 = 'Life,is,your,creation'; String str3 = 'Hello! how are you?'; String[] stringarray1 = str1.split('8',2); System.out.println('When the limit is positive:'); System.out.println('Number of tokens: '+stringarray1.length); for(int i=0; i<stringarray1.length; i++) { system.out.println(stringarray1[i]); } string[] stringarray2="str2.split('y',-3);" system.out.println(\' when the limit is negative: \'); system.out.println(\'number of tokens: \'+stringarray2.length); for(int i="0;" i<stringarray2.length; system.out.println(stringarray2[i]); stringarray3="str3.split('!',0);" equal to 0:\'); \'+stringarray3.length); i<stringarray3.length; system.out.println(stringarray3[i]); < pre> <p> <strong>Output:</strong> </p> <pre> When the limit is positive: Number of tokens: 2 46 -567-7388 When the limit is negative: Number of tokens: 2 Life,is, our,creation When the limit is equal to 0: Number of tokens: 2 Hello how are you? </pre> <p>In the above code snippet, we see that:</p> <ul> <li>When the limit is 2, the number of tokens in the string array is two.</li> <li>When the limit is -3, the specified string is split into 2 tokens. It includes the trailing spaces.</li> <li>When the limit is 0, the specified string is split into 2 tokens. In this case, trailing space is omitted.</li> </ul> <h3>Example of Pipe Delimited String</h3> <p>Splitting a string delimited by pipe (|) is a little bit tricky. Because the pipe is a special character in Java regular expression.</p> <p>Let's create a string delimited by pipe and split it by pipe.</p> <p> <strong>SplitStringExample5.java</strong> </p> <pre> public class SplitStringExample5 { public static void main(String args[]) { //defining a String object String s = 'Life|is|your|creation'; //split string delimited by comma String[] stringarray = s.split('|'); //we can use dot, whitespace, any character //iterate over string array for(int i=0; i<stringarray.length; i++) { prints the tokens system.out.println(stringarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> L i f e | i s | y o u r | c r e a t i o n </pre> <p>In the above example, we see that it does not produce the same output as other delimiter yields. It should produce an array of tokens, <strong>life, yours,</strong> and <strong>creation</strong> , but it is not. It gives the result, as we have seen in the output above.</p> <p>The reason behind it that the regular expression engine interprets the pipe delimiter as a <strong>Logical OR operator</strong> . The regex engine splits the String on empty String.</p> <p>In order to resolve this problem, we must <strong>escape</strong> the pipe character when passed to the split() method. We use the following statement to escape the pipe character:</p> <pre> String[] stringarray = s.split('\|'); </pre> <p>Add a pair of <strong>backslash (\)</strong> before the delimiter to escape the pipe. After doing the changes in the above program, the regex engine interprets the pipe character as a delimiter.</p> <p>Another way to escape the pipe character is to put the pipe character inside a pair of square brackets, as shown below. In the Java regex API, the pair of square brackets act as a character class.</p> <pre> String[] stringarray = s.split('[|]'); </pre> <p>Both the above statements yield the following output:</p> <p> <strong>Output:</strong> </p> <pre> Life is your creation </pre> <h3>Using StringTokenizer Class</h3> <p>Java <strong>StringTokenizer</strong> is a legacy class that is defined in java.util package. It allows us to split the string into tokens. It is not used by the programmer because the split() method of the String class does the same work. So, the programmer prefers the split() method instead of the StringTokenizer class. We use the following two methods of the class:</p> <p> <strong>StringTokenizer.hasMoreTokens()</strong> </p> <p>The method iterates over the string and checks if there are more tokens available in the tokenizer string. It returns true if there is one token is available in the string after the current position, else returns false. It internally calls the <strong>nextToken()</strong> method if it returns true and the nextToken() method returns the token.</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean hasMoreTokens() </pre> <p> <strong>StringTokenizer.nextToken()</strong> </p> <p>It returns the next token from the string tokenizer. It throws <strong>NoSuchElementException</strong> if the tokens are not available in the string tokenizer.</p> <p> <strong>Syntax:</strong> </p> <pre> public String nextToken() </pre> <p>Let's create a program that splits the string using the StringTokenizer class.</p> <p> <strong>SplitStringExample6.java</strong> </p> <pre> import java.util.StringTokenizer; public class SplitStringExample6 { public static void main(String[] args) { //defining a String object String str = 'Welcome/to/Javatpoint'; //constructor of the StringTokenizer class StringTokenizer tokens = new StringTokenizer(str, '/'); //checks if the string has more tokens or not while (tokens.hasMoreTokens()) { //prints the tokens System.out.println(tokens.nextToken()); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Welcome to Javatpoint </pre> <h2>Using Scanner.useDelimiter() Method</h2> <p>Java <strong>Scanner</strong> class provides the <strong>useDelimiter()</strong> method to split the string into tokens. There are two variants of the useDelimiter() method:</p> <ul> <li>useDelimiter(Pattern pattern)</li> <li>useDelimiter(String pattern)</li> </ul> <h3>useDelimiter(Pattern pattern)</h3> <p>The method sets the scanner's delimiting pattern to the specified string. It parses a delimiting pattern as an argument. It returns the Scanner.</p> <p> <strong>Syntax:</strong> </p> <pre> public Scanner useDelimiter(Pattern pattern) </pre> <h3>useDelimiter(String pattern)</h3> <p>The method sets the scanner's delimiting pattern to a pattern that constructs from the specified string. It parses a delimiting pattern as an argument. It returns the Scanner.</p> <p> <strong>Syntax:</strong> </p> <pre> public Scanner useDelimiter(String pattern) </pre> <h4>Note: Both the above methods behave in the same way, as invoke the useDelimiter(Pattern.compile(pattern)).</h4> <p>In the following program, we have used the useDelimiter() method to split the string.</p> <p> <strong>SplitStringExample7.java</strong> </p> <pre> import java.util.Scanner; public class SplitStringExample7 { public static void main(String args[]) { //construtor of the Scanner class Scanner scan = new Scanner('Do/your/work/self'); //Initialize the string delimiter scan.useDelimiter('/'); //checks if the tokenized Strings has next token while(scan.hasNext()) { //prints the next token System.out.println(scan.next()); } //closing the scanner scan.close(); } } </pre> <p> <strong>Output:</strong> </p> <pre> Do your work self </pre> <hr></stringarray.length;></pre></stringarray1.length;></pre></stringarray.length;>
String.split(String regex, limite interno)
Ele nos permite dividir a string especificada pelo delimitador, mas em um número limitado de tokens. O método aceita dois parâmetros regex (uma expressão regular delimitadora) e limit. O parâmetro limit é usado para controlar o número de vezes que o padrão é aplicado e afeta a matriz resultante. Ele retorna uma matriz de objetos String calculados dividindo a string fornecida de acordo com o parâmetro limit.
Há uma pequena diferença entre a variante dos métodos split() que limita o número de tokens retornados após invocar o método.
Sintaxe:
public String[] split(String regex, int limit)
Isso joga PatternSyntaxException se a expressão regular analisada tiver uma sintaxe inválida.
O parâmetro limite pode ser positivo, negativo ou igual ao limite.
SplitStringExample4.java
public class SplitStringExample4 { public static void main(String args[]) { String str1 = '468-567-7388'; String str2 = 'Life,is,your,creation'; String str3 = 'Hello! how are you?'; String[] stringarray1 = str1.split('8',2); System.out.println('When the limit is positive:'); System.out.println('Number of tokens: '+stringarray1.length); for(int i=0; i<stringarray1.length; i++) { system.out.println(stringarray1[i]); } string[] stringarray2="str2.split('y',-3);" system.out.println(\' when the limit is negative: \'); system.out.println(\'number of tokens: \'+stringarray2.length); for(int i="0;" i<stringarray2.length; system.out.println(stringarray2[i]); stringarray3="str3.split('!',0);" equal to 0:\'); \'+stringarray3.length); i<stringarray3.length; system.out.println(stringarray3[i]); < pre> <p> <strong>Output:</strong> </p> <pre> When the limit is positive: Number of tokens: 2 46 -567-7388 When the limit is negative: Number of tokens: 2 Life,is, our,creation When the limit is equal to 0: Number of tokens: 2 Hello how are you? </pre> <p>In the above code snippet, we see that:</p> <ul> <li>When the limit is 2, the number of tokens in the string array is two.</li> <li>When the limit is -3, the specified string is split into 2 tokens. It includes the trailing spaces.</li> <li>When the limit is 0, the specified string is split into 2 tokens. In this case, trailing space is omitted.</li> </ul> <h3>Example of Pipe Delimited String</h3> <p>Splitting a string delimited by pipe (|) is a little bit tricky. Because the pipe is a special character in Java regular expression.</p> <p>Let's create a string delimited by pipe and split it by pipe.</p> <p> <strong>SplitStringExample5.java</strong> </p> <pre> public class SplitStringExample5 { public static void main(String args[]) { //defining a String object String s = 'Life|is|your|creation'; //split string delimited by comma String[] stringarray = s.split('|'); //we can use dot, whitespace, any character //iterate over string array for(int i=0; i<stringarray.length; i++) { prints the tokens system.out.println(stringarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> L i f e | i s | y o u r | c r e a t i o n </pre> <p>In the above example, we see that it does not produce the same output as other delimiter yields. It should produce an array of tokens, <strong>life, yours,</strong> and <strong>creation</strong> , but it is not. It gives the result, as we have seen in the output above.</p> <p>The reason behind it that the regular expression engine interprets the pipe delimiter as a <strong>Logical OR operator</strong> . The regex engine splits the String on empty String.</p> <p>In order to resolve this problem, we must <strong>escape</strong> the pipe character when passed to the split() method. We use the following statement to escape the pipe character:</p> <pre> String[] stringarray = s.split('\|'); </pre> <p>Add a pair of <strong>backslash (\)</strong> before the delimiter to escape the pipe. After doing the changes in the above program, the regex engine interprets the pipe character as a delimiter.</p> <p>Another way to escape the pipe character is to put the pipe character inside a pair of square brackets, as shown below. In the Java regex API, the pair of square brackets act as a character class.</p> <pre> String[] stringarray = s.split('[|]'); </pre> <p>Both the above statements yield the following output:</p> <p> <strong>Output:</strong> </p> <pre> Life is your creation </pre> <h3>Using StringTokenizer Class</h3> <p>Java <strong>StringTokenizer</strong> is a legacy class that is defined in java.util package. It allows us to split the string into tokens. It is not used by the programmer because the split() method of the String class does the same work. So, the programmer prefers the split() method instead of the StringTokenizer class. We use the following two methods of the class:</p> <p> <strong>StringTokenizer.hasMoreTokens()</strong> </p> <p>The method iterates over the string and checks if there are more tokens available in the tokenizer string. It returns true if there is one token is available in the string after the current position, else returns false. It internally calls the <strong>nextToken()</strong> method if it returns true and the nextToken() method returns the token.</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean hasMoreTokens() </pre> <p> <strong>StringTokenizer.nextToken()</strong> </p> <p>It returns the next token from the string tokenizer. It throws <strong>NoSuchElementException</strong> if the tokens are not available in the string tokenizer.</p> <p> <strong>Syntax:</strong> </p> <pre> public String nextToken() </pre> <p>Let's create a program that splits the string using the StringTokenizer class.</p> <p> <strong>SplitStringExample6.java</strong> </p> <pre> import java.util.StringTokenizer; public class SplitStringExample6 { public static void main(String[] args) { //defining a String object String str = 'Welcome/to/Javatpoint'; //constructor of the StringTokenizer class StringTokenizer tokens = new StringTokenizer(str, '/'); //checks if the string has more tokens or not while (tokens.hasMoreTokens()) { //prints the tokens System.out.println(tokens.nextToken()); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Welcome to Javatpoint </pre> <h2>Using Scanner.useDelimiter() Method</h2> <p>Java <strong>Scanner</strong> class provides the <strong>useDelimiter()</strong> method to split the string into tokens. There are two variants of the useDelimiter() method:</p> <ul> <li>useDelimiter(Pattern pattern)</li> <li>useDelimiter(String pattern)</li> </ul> <h3>useDelimiter(Pattern pattern)</h3> <p>The method sets the scanner's delimiting pattern to the specified string. It parses a delimiting pattern as an argument. It returns the Scanner.</p> <p> <strong>Syntax:</strong> </p> <pre> public Scanner useDelimiter(Pattern pattern) </pre> <h3>useDelimiter(String pattern)</h3> <p>The method sets the scanner's delimiting pattern to a pattern that constructs from the specified string. It parses a delimiting pattern as an argument. It returns the Scanner.</p> <p> <strong>Syntax:</strong> </p> <pre> public Scanner useDelimiter(String pattern) </pre> <h4>Note: Both the above methods behave in the same way, as invoke the useDelimiter(Pattern.compile(pattern)).</h4> <p>In the following program, we have used the useDelimiter() method to split the string.</p> <p> <strong>SplitStringExample7.java</strong> </p> <pre> import java.util.Scanner; public class SplitStringExample7 { public static void main(String args[]) { //construtor of the Scanner class Scanner scan = new Scanner('Do/your/work/self'); //Initialize the string delimiter scan.useDelimiter('/'); //checks if the tokenized Strings has next token while(scan.hasNext()) { //prints the next token System.out.println(scan.next()); } //closing the scanner scan.close(); } } </pre> <p> <strong>Output:</strong> </p> <pre> Do your work self </pre> <hr></stringarray.length;></pre></stringarray1.length;>
No trecho de código acima, vemos que:
- Quando o limite é 2, o número de tokens na matriz de strings é dois.
- Quando o limite é -3, a string especificada é dividida em 2 tokens. Inclui os espaços finais.
- Quando o limite é 0, a string especificada é dividida em 2 tokens. Neste caso, o espaço à direita é omitido.
Exemplo de string delimitada por barra vertical
Dividir uma string delimitada por barra vertical (|) é um pouco complicado. Porque o pipe é um caractere especial na expressão regular Java.
Vamos criar uma string delimitada por barra vertical e dividi-la por barra vertical.
SplitStringExample5.java
public class SplitStringExample5 { public static void main(String args[]) { //defining a String object String s = 'Life|is|your|creation'; //split string delimited by comma String[] stringarray = s.split('|'); //we can use dot, whitespace, any character //iterate over string array for(int i=0; i<stringarray.length; i++) { prints the tokens system.out.println(stringarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> L i f e | i s | y o u r | c r e a t i o n </pre> <p>In the above example, we see that it does not produce the same output as other delimiter yields. It should produce an array of tokens, <strong>life, yours,</strong> and <strong>creation</strong> , but it is not. It gives the result, as we have seen in the output above.</p> <p>The reason behind it that the regular expression engine interprets the pipe delimiter as a <strong>Logical OR operator</strong> . The regex engine splits the String on empty String.</p> <p>In order to resolve this problem, we must <strong>escape</strong> the pipe character when passed to the split() method. We use the following statement to escape the pipe character:</p> <pre> String[] stringarray = s.split('\|'); </pre> <p>Add a pair of <strong>backslash (\)</strong> before the delimiter to escape the pipe. After doing the changes in the above program, the regex engine interprets the pipe character as a delimiter.</p> <p>Another way to escape the pipe character is to put the pipe character inside a pair of square brackets, as shown below. In the Java regex API, the pair of square brackets act as a character class.</p> <pre> String[] stringarray = s.split('[|]'); </pre> <p>Both the above statements yield the following output:</p> <p> <strong>Output:</strong> </p> <pre> Life is your creation </pre> <h3>Using StringTokenizer Class</h3> <p>Java <strong>StringTokenizer</strong> is a legacy class that is defined in java.util package. It allows us to split the string into tokens. It is not used by the programmer because the split() method of the String class does the same work. So, the programmer prefers the split() method instead of the StringTokenizer class. We use the following two methods of the class:</p> <p> <strong>StringTokenizer.hasMoreTokens()</strong> </p> <p>The method iterates over the string and checks if there are more tokens available in the tokenizer string. It returns true if there is one token is available in the string after the current position, else returns false. It internally calls the <strong>nextToken()</strong> method if it returns true and the nextToken() method returns the token.</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean hasMoreTokens() </pre> <p> <strong>StringTokenizer.nextToken()</strong> </p> <p>It returns the next token from the string tokenizer. It throws <strong>NoSuchElementException</strong> if the tokens are not available in the string tokenizer.</p> <p> <strong>Syntax:</strong> </p> <pre> public String nextToken() </pre> <p>Let's create a program that splits the string using the StringTokenizer class.</p> <p> <strong>SplitStringExample6.java</strong> </p> <pre> import java.util.StringTokenizer; public class SplitStringExample6 { public static void main(String[] args) { //defining a String object String str = 'Welcome/to/Javatpoint'; //constructor of the StringTokenizer class StringTokenizer tokens = new StringTokenizer(str, '/'); //checks if the string has more tokens or not while (tokens.hasMoreTokens()) { //prints the tokens System.out.println(tokens.nextToken()); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Welcome to Javatpoint </pre> <h2>Using Scanner.useDelimiter() Method</h2> <p>Java <strong>Scanner</strong> class provides the <strong>useDelimiter()</strong> method to split the string into tokens. There are two variants of the useDelimiter() method:</p> <ul> <li>useDelimiter(Pattern pattern)</li> <li>useDelimiter(String pattern)</li> </ul> <h3>useDelimiter(Pattern pattern)</h3> <p>The method sets the scanner's delimiting pattern to the specified string. It parses a delimiting pattern as an argument. It returns the Scanner.</p> <p> <strong>Syntax:</strong> </p> <pre> public Scanner useDelimiter(Pattern pattern) </pre> <h3>useDelimiter(String pattern)</h3> <p>The method sets the scanner's delimiting pattern to a pattern that constructs from the specified string. It parses a delimiting pattern as an argument. It returns the Scanner.</p> <p> <strong>Syntax:</strong> </p> <pre> public Scanner useDelimiter(String pattern) </pre> <h4>Note: Both the above methods behave in the same way, as invoke the useDelimiter(Pattern.compile(pattern)).</h4> <p>In the following program, we have used the useDelimiter() method to split the string.</p> <p> <strong>SplitStringExample7.java</strong> </p> <pre> import java.util.Scanner; public class SplitStringExample7 { public static void main(String args[]) { //construtor of the Scanner class Scanner scan = new Scanner('Do/your/work/self'); //Initialize the string delimiter scan.useDelimiter('/'); //checks if the tokenized Strings has next token while(scan.hasNext()) { //prints the next token System.out.println(scan.next()); } //closing the scanner scan.close(); } } </pre> <p> <strong>Output:</strong> </p> <pre> Do your work self </pre> <hr></stringarray.length;>
No exemplo acima, vemos que ele não produz a mesma saída que outros rendimentos de delimitadores. Deve produzir uma série de tokens, vida, sua, e criação , mas não é. Dá o resultado, como vimos na saída acima.
A razão por trás disso é que o mecanismo de expressão regular interpreta o delimitador de barra vertical como um Operador lógico OR . O mecanismo regex divide a String em uma String vazia.
Para resolver este problema, devemos escapar o caractere de barra vertical quando passado para o método split(). Usamos a seguinte instrução para escapar do caractere de barra vertical:
String[] stringarray = s.split('\|');
Adicione um par de barra invertida (\) antes do delimitador para escapar do tubo. Depois de fazer as alterações no programa acima, o mecanismo regex interpreta a barra vertical como um delimitador.
Outra maneira de escapar da barra vertical é colocá-la entre colchetes, conforme mostrado abaixo. Na API Java regex, o par de colchetes atua como uma classe de caracteres.
String[] stringarray = s.split('[|]');
Ambas as declarações acima produzem a seguinte saída:
texto datilografado do loop foreach
Saída:
Life is your creation
Usando a classe StringTokenizer
Java StringTokenizer é uma classe legada definida no pacote java.util. Isso nos permite dividir a string em tokens. Não é usado pelo programador porque o método split() da classe String faz o mesmo trabalho. Portanto, o programador prefere o método split() em vez da classe StringTokenizer. Usamos os dois métodos da classe a seguir:
StringTokenizer.hasMoreTokens()
O método itera sobre a string e verifica se há mais tokens disponíveis na string do tokenizer. Ele retorna verdadeiro se houver um token disponível na string após a posição atual, caso contrário, retorna falso. Chama internamente o próximoToken() método se retornar verdadeiro e o método nextToken() retornar o token.
Sintaxe:
public boolean hasMoreTokens()
StringTokenizer.nextToken()
ator saira banu
Ele retorna o próximo token do tokenizer de string. Isso joga NoSuchElementException se os tokens não estiverem disponíveis no tokenizer de string.
Sintaxe:
public String nextToken()
Vamos criar um programa que divida a string usando a classe StringTokenizer.
SplitStringExample6.java
import java.util.StringTokenizer; public class SplitStringExample6 { public static void main(String[] args) { //defining a String object String str = 'Welcome/to/Javatpoint'; //constructor of the StringTokenizer class StringTokenizer tokens = new StringTokenizer(str, '/'); //checks if the string has more tokens or not while (tokens.hasMoreTokens()) { //prints the tokens System.out.println(tokens.nextToken()); } } }
Saída:
Welcome to Javatpoint
Usando o método Scanner.useDelimiter()
Java Scanner classe fornece o useDelimitador() método para dividir a string em tokens. Existem duas variantes do método useDelimiter():
- useDelimiter (padrão padrão)
- useDelimiter (padrão de string)
useDelimiter (padrão padrão)
O método define o padrão de delimitação do scanner para a string especificada. Ele analisa um padrão delimitador como um argumento. Ele retorna o Scanner.
Sintaxe:
public Scanner useDelimiter(Pattern pattern)
useDelimiter (padrão de string)
O método define o padrão de delimitação do scanner para um padrão construído a partir da string especificada. Ele analisa um padrão delimitador como um argumento. Ele retorna o Scanner.
Sintaxe:
public Scanner useDelimiter(String pattern)
Nota: Ambos os métodos acima se comportam da mesma maneira, pois invocam useDelimiter(Pattern.compile(pattern)).
No programa a seguir, usamos o método useDelimiter() para dividir a string.
SplitStringExample7.java
import java.util.Scanner; public class SplitStringExample7 { public static void main(String args[]) { //construtor of the Scanner class Scanner scan = new Scanner('Do/your/work/self'); //Initialize the string delimiter scan.useDelimiter('/'); //checks if the tokenized Strings has next token while(scan.hasNext()) { //prints the next token System.out.println(scan.next()); } //closing the scanner scan.close(); } }
Saída:
Do your work self