How to separate a string string by spaces? [duplicate]

-1

In Java code, I want to know how I can separate a text string for each space that exists in it.

    
asked by Juan Camilo Moreno Alzate 01.09.2017 в 04:22
source

2 answers

2

Use the split() method that separates the string with the string specified in the parameter:

String[] datos = "hola mundo como estas".split(" ");

for(String item : datos)
{
  System.out.println(item);
}
    
answered by 01.09.2017 в 04:26
0

You can use this function:

is being used on a string S

s="Separar por espacios"
s.split(" ");

The split () function can be used not only for one character but for several.

Example:

String cadena = "texto de que quieras";
String delimitadores= "[ .,;?!¡¿\'\"\[\]]+";
String[] palabrasSeparadas = cadena.split(delimitadores);
    
answered by 01.09.2017 в 04:38