how to check if string contains '+' character

user93796 picture user93796 · Apr 8, 2012 · Viewed 238.8k times · Source

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?

Answer

Eng.Fouad picture Eng.Fouad · Apr 8, 2012

You need this instead:

if(s.contains("+"))

contains() method of String class does not take regular expression as a parameter, it takes normal text.


EDIT:

String s = "ddjdjdj+kfkfkf";

if(s.contains("+"))
{
    String parts[] = s.split("\\+");
    System.out.print(parts[0]);
}

OUTPUT:

ddjdjdj