Tuesday 15 June 2010

java - How to get only matched substring start , end indexes from string object -



java - How to get only matched substring start , end indexes from string object -

i want utilize wild card functionality,

if pattern * test* , string abctestbcd have start index=3, endindex=6

try code:

string searchstring = "test"; string s = "abctestbcd"; int startindex = s.indexof("test"); int endindex = startindex+searchstring.length()-1; system.out.println(startindex); system.out.println(endindex);

if possible hat multiple matches utilize in loop:

string searchstring = "test"; string s = "abctestbcdtest"; int startindex = -1; { startindex = s.indexof("test", startindex+1); int endindex = startindex+searchstring.length()-1; if (startindex != -1){ system.out.println(startindex); system.out.println(endindex); } } while(startindex != -1);

java regex

No comments:

Post a Comment