Skip to content
Advertisement

Check for a Substring after a particular occurrence of string which is separated by dots

I have a string with dots. I want to get a particular substring after occurrence of word

entity

that is for example

String givenStr = "com.web.rit.entity.TestName.create";
output - TestName

String givenStr = "com.web.rit.entity.TestName2.update";
output - TestName2

So as explained above I have to extract substring after the string entity from the given string. Can anyone please help? (I am using java to do it).

Advertisement

Answer

You can do something like this :

String givenStr = "com.web.rit.entity.TestName.create"; // begin str
String wording = "entity"; // looking for begin
String[] splitted = givenStr.split("\."); // get all args
for(int i = 0; i < splitted.length; i++) {
    if(splitted[i].equalsIgnoreCase(wording)) { // checking if it's what is required
        System.out.println("Output: " + splitted[i + 1]); // should not be the last item, else you will get error. You can add if arg before to fix it
        return;
   }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement