Kohnarik Kohnarik - 1 year ago 137
Java Question

Java get special characters from string

Given a random String, I want to be able to extract certain characters from my string (

), one after each other.

Let's say the String is

How can I retrieve those 4 operators?

If I had to extract everything between those operators, I would use
, but if I had to do it the other way around, how would I achieve that?

The amount of characters between those operators can vary heavily.

Answer Source

You can use a regex to remove all characters but +, -, *, /:

String text = "wejri+234-wer*324sdf/";
String res = text.replaceAll("[^+\\-\\*/]", "");   // - and * need to be escaped
// > res = "+-*/"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download