I want to check if my string contains a + character.I tried following code
s= "ddjdjdj+kfkfkf";
if(s.contains ("\\+"){
String parts[] = s.split("\\+);
s= parts[0]; // i want to strip part after +
}
but it doesnot give expected result.Any idea?
[+]is simpler
You need this instead:
contains()
method ofString
class does not take regular expression as a parameter, it takes normal text.EDIT:
OUTPUT:
Why not just:
It's not really clear why your original version didn't work, but then you didn't say what actually happened. If you want to split not using regular expressions, I'd personally use Guava:
If you're going to use
split
(either the built-in version or Guava) you don't need to check whether it contains+
first - if it doesn't there'll only be one result anyway. Obviously there's a question of efficiency, but it's simpler code:Note that writing
String[] parts
is preferred overString parts[]
- it's much more idiomatic Java code.