Search code examples
javaregexapache-commons

How can i find whether the string starts with 's','r','p' in java


Example: This is my string,

String sample = "s5656";

If the first character of the string contains 's' or 'p' or 'r' means i should remove the character,Otherwise i have to return the original string.

Is there any optimized way to do that like "regex" or "StringUtils" in apache common?


Solution

  • Why do you want to add 3rd party jar for this kind of simple requirement? You can try as follows

    String sample = "s5656";
    if(sample.startsWith("s")||sample.startsWith("r")||sample.startsWith("p")){
      // do necessary 
    }else{
      // do necessary 
    }
    

    String#startsWith()