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

Recommended Answers

All 2 Replies

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)

Brilliant I never thought of that , thats great

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.