I recently wanted to calculate all occurrences of a substring in a string with Dart. The code I used is nearly the same as you would use in Java to do this. So here’s my snippet for Dart:
int countOccurences(mainString, search) { int lInx = 0; int count =0; while(lInx != -1){ lInx = mainString.indexOf(search,lInx); if( lInx != -1){ count++; lInx+=search.length; } } return count; }
You can now simply call it like that:
int countAll = countOccurences(sameRandomString, "smile");
Recent Comments