0

Hello if a have the string:String myString = "This is my good string.,

I want to check if the word "my" is contained in myString, and so i want to transform myString in myString = "my good string.

I tryied this code per verify the subrstring in the string:

String myString = "this is my good string.";
String mySubString = "my";

if (myString.toLowerCase().contains(mySubString.toLowerCase()) {
  return true;
}

But now how can i take myString from "my..." to the end of that String? Thank you!

1
  • 1
    Use: int positionOfMy = myString.indexOf("my"); to get position of "my" and String myTransformedString = myString.substring(positionOfMy); to get substring you're looking for Commented Feb 12, 2017 at 15:15

3 Answers 3

1

First find out the index of word "my"from your original string.

int i = myString.indexOf(mySubString);

Then reassign your string to a substring that will begin at that index until the end of string.

myString = myString.substring(i);
Sign up to request clarification or add additional context in comments.

Comments

0

With regex :

myString = myString.replaceFirst(".*(?=my)", "");

Comments

0

String s="this is my good string";

indexOf() finds the starting index of "my". substring() finds the String, that starts with "my".

    String st=s.substring(s.indexOf("my",0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.