Thursday, October 9, 2008

A sample about string class

using System.Collections;//

namespace StringClass
{
class Program
{
static void Main(string[] args)
{
string input = " now is the good time to have a trip ";
ArrayList words = new ArrayList();
words = SplitWords(input);

foreach (string word in words)
{
Console.WriteLine(word);
}
Console.WriteLine("....");
}

public static ArrayList SplitWords(string astring)
{
ArrayList words = new ArrayList();
int pos;
string word;
astring = astring.Trim();

pos = astring.IndexOf(" ");
while (pos > 0)
{
word = astring.Substring(0, pos);
words.Add(word);
astring = astring.Substring(pos + 1, astring.Length - (pos + 1));
pos = astring.IndexOf(" ");
if (pos < 0)
{
word = astring.Substring(0, astring.Length);
words.Add(word);
}
}

return words;
}
}
}

No comments: