Does anybody know the simplest and cleanest way of of counting the number of times a substring occurs in a string in Java?
Is there also a way to accomplishing the task using the regular expressions?
Does anybody know the simplest and cleanest way of of counting the number of times a substring occurs in a string in Java?
Is there also a way to accomplishing the task using the regular expressions?
I believe that you can, one of the objects returns an array of matches. I don't remember exactly, but it is doable (unless I am not remembering something).
I am infallible, you should know that by now.
"My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
(02:15:31 PM) ***TimToady and snake oil go way back...
42 lines of Perl - SHI - Home Site
Regexps are probably the easiest one... you just call "find" as many times as it returns something, and count those.
LambdaGrok. | #ubuntu-programming on FreeNode
You could use String.indexOf(string,start)
Like so:
Where str is the string you are searching in and findStr is what you are looking for.
That would do exactly what you need. There may be more efficient solutions out there like regular expressions though. This one is decent though.Code:lastIndex = 0; count =0; while(lastIndex != -1){ lastIndex = str.indexOf(findStr,lastIndex); if( lastIndex != -1){ count ++; } }
Last edited by themusicwave; April 12th, 2008 at 04:29 AM. Reason: Forgot my code tags
Thank you all. Everything was very helpful. I stuck with the regular expression option.
You would have to change this line to ensure subsequent iterations don't just keep looking at the previous result, but yeah, regular expressions sound like the go since I'm wanting to perform validation tasks.
Thanks.
Update:
Wanting to count the number of commas in a line of CSV text.
Code:import java.util.regex.Pattern; public static Boolean isValid(final String s) { String regex = "[^,]*,[^,]*,[^,]*,[^,]*,[^,]*"; return Pattern.matches(regex, s); }
Last edited by sav991; August 4th, 2010 at 04:54 AM. Reason: Problem-proper solved
Go you good thing!
Regexes might be overkill for counting commas.
If you don't care about commas in quoted strings, or any escape characters, you can just loop over the results of stringVariable.toCharArray() incrementing a variable whenever you see a comma. Linear time, constant space cost.
If you do care, something like
Code:boolean escapeMode = false; // \ = escape next character boolean quoteMode = false; // "foo" int count = 0; // note: below does necessarily not handle all // multi-byte Unicode characters; it also does not handle // quote characters except " // // if that is a concern, use String's getCodePointAt() // and Character's toChars(), as well as Character's // getType() for identifying initial/final quotes... for (char c : stringVariable.toCharArray()) { if (escapeMode) { // Only good for one character. escapeMode = false; } else if (c == '\\') { // next character is treated as normal even if special // even if we're in a quote; this allows a quote within // a quoted string escapeMode = true; } else if (c == '"') { quoteMode = !quoteMode; } else if ((!quoteMode) && (c == ',')) { count++; } }
Bookmarks