HOW TO FIND STRING STARTS WORD IN JAVA?

jacob rasel

Total Post:88

Points:616
Posted by  jacob rasel
Java 
 908  View(s)
Ratings:
Rate this:
Example: This is my string,

String sample = "random";
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?
  1. ben reitman

    Post:96

    Points:676
    Re: How to find string starts word in java?

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

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!