Exemplo n.º 1
0
        public static int WordPosition(int n, String s, ACharArray wordDelims)
        {
            var count  = 0;
            var i      = 0;
            var result = 0;

            while (i < s.Length && count != n)
            {
                /* skip over delimiters */
                while ((i < s.Length) && (wordDelims.Contains(s[i])))
                {
                    i++;
                }
                /* if we're not beyond end of S, we're at the start of a word */
                if (i < s.Length)
                {
                    count++;
                }
                /* if not finished, find the end of the current word */
                if (count != n)
                {
                    while ((i < s.Length) && !(wordDelims.Contains(s[i])))
                    {
                        i++;
                    }
                }
                else
                {
                    result = i;
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public static int WordCount(String source, ACharArray wordDelims)
        {
            var result = 0;
            var i      = 0;
            var lev    = source.Length;

            while (i < lev)
            {
                while (i < lev && wordDelims.Contains(source[i]))
                {
                    i += 1;
                }
                if (i <= lev)
                {
                    result += 1;
                }
                while (i < lev && !wordDelims.Contains(source[i]))
                {
                    i += 1;
                }
            }
            return(result);
        }
Exemplo n.º 3
0
 public static String ExtractWord(int n, String s, ACharArray wordDelims)
 {
     using (var sb = new UStringBuilder())
     {
         var i = WordPosition(n, s, wordDelims);
         if (i == -1)
         {
             return(sb.ToString());
         }
         /* find the end of the current word */
         while (i < s.Length && !(wordDelims.Contains(s[i])))
         {
             /* add the I'th character to result */
             sb.Append(s[i]);
             i++;
         }
         return(sb.ToString());
     }
 }