Reading String until you find a certain character

2

I can receive data like:

  

232321   or   dwd

But sometimes a "+" may appear between the Strings. How do I extract only what comes BEFORE this character?

If so, I have an entry like this: "432d + 321" I would stay: 432d.

    
asked by anonymous 18.07.2016 / 18:04

2 answers

5
String str = "432d+321";
String res = str.split("\+")[0];

// res = 432d.

Note: For meta caracteres , you should use '\' .

\ , ^ , $ , * , + , ?
    
18.07.2016 / 18:23
4

Do this:

String str = "432d+321";

int posicao = str.indexOf('+');
if (posicao >= 0) {
    str = str.substring(0, posicao);
}

System.out.println(str); // Imprime 432d
    
18.07.2016 / 18:20