Wednesday, 15 April 2015

Counting occurences of substring ignoring case in JAVA -



Counting occurences of substring ignoring case in JAVA -

i trying count occurences of div tag in html file. when search div, 2 , div, 1650. ideally when utilize shtml.touppercase(), , search div, should 1652. getting 1656. might going wrong here?

/********* counting occurences of div **************/ string findstring = "div"; int lastindex = 0; int count = 0; while (lastindex != -1) { lastindex = shtml.indexof(findstring, lastindex); if (lastindex != -1) { count++; lastindex += findstring.length(); } } system.out.println("count of div = " + count);

you picking substrings mixed-case before - say, div. not reason count "div"s, though, because pick parts of longer words (say, division or divorce).

if want improve count, utilize simple regex counting:

"[</]div[ />]"

this regular look match div preceded < or /, , followed space, /, or >:

pattern countrx = pattern.compile("[</]div[ />]", pattern.case_insensitive); matcher m = countrx.matcher(shtml); int count = 0; while (m.find()) { count++; } system.out.println(count);

java substring

No comments:

Post a Comment