By | 13 December 2011

Word Count in PeopleCode Using Regular Expressions

Here is a quick function that will calculate the word count of a string using Java Regular expressions.

Function wordcountjava(&inString as string) returns integer

   Local JavaObject &patternClass = GetJavaClass("java.util.regex.Pattern");
   Local JavaObject &pattern = &patternClass.compile("(\S+)");
   Local JavaObject &jstring = CreateJavaObject("java.lang.String", &inString);
   Local JavaObject &matches = &pattern.matcher(&jstring.subSequence(0, &jstring.length()));
   Local integer &i;
   While &matches.find()
      &i = &i + 1;
   return &i;

Article Categories

Do you want to become a CI Expert?

If you want to learn more about developing using Component Interface then check out our CI Training Video.