0

Hi Guys

I wonder if you can help me get my head around this.

I am trying to search a String for a sub string but there are not gaps in the original string for example

String s1 = "www.google.com/search/"
String s2 = "google"

when i attempt to use matches class it wont show that the first string contains google, I have been told that regex should hold the answer, I was just wondering if anyone has any ideas on how this can be done.

Thanks

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by steveh000
0

Check out the indexOf method of the String class:

String s1 = "www.google.com/search/"
String s2 = "google"

int index = s1.indexOf(s2);
System.out.println(index); // 4

If "index" is -1 then the s2 is not inside the s1. Else it returns the position of the s2 inside the s1, which is 4. "google" starts at the 4th character in the s1 String (starting from 0)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.