Reading String until you find a certain character

Asked

Viewed 2,043 times

2

I can receive data like:

232321 or dwd

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

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

2 answers

6


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

// res = 432d.

Obs: for meta caracteres, should use the '\\'.

\ , ^ , $ , * , + , ?

5

Do so:

String str = "432d+321";

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

System.out.println(str); // Imprime 432d

Browser other questions tagged

You are not signed in. Login or sign up in order to post.