Re: Help ArrayLis won't store, and how to remove duplicates????
From: Bob Grommes (bob_at_bobgrommes.com)
Date: 08/22/04
- Next message: freddy: "RE: C# & ADO Book(s)"
- Previous message: Bob Grommes: "Re: Rich text box question."
- Next in thread: Bob Grommes: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Maybe reply: Bob Grommes: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Maybe reply: Jon Skeet [C# MVP]: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Maybe reply: Etienne Boucher: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Maybe reply: Jon Skeet [C# MVP]: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 22 Aug 2004 09:31:07 -0700
Split() is a very simplistic method. It can split on a single character, or
on multiple characters (have a look at the Split(char[]) override) but if
you have for instance quoted strings within which delimiters must be
ignored, as with:
Fred,Smith,"Stockton, MD"
... or other special cases, the you'll have to write your own routine using
string manipulation methods and/or Regex.
Your code looks fine offhand. If you can provide your input.txt file and a
sample of the bad output that would be helpful in diagnosing your problem.
Incidentally, foreach does not guarantee that it will traverse a collection
in order, though in my experience it does do so with arrays and ArrayLists.
Still, before concluding that the ArrayList is not really sorted, I'd use a
for loop instead. Or look at it in the debugger.
As for capitalizing the first letter of each word -- you have to write a
method for that yourself. Simplistically, something like:
string ToProperCase(string strWord) {
if (strWord.Length == 0) {
return "";
} else if (strWord.Length == 1) {
return strWord.ToUpper();
} else {
return Char.ToUpper(strWord[0]).ToString() + strWord.Substring(1);
}
}
--Bob
"steve smith" <boy_wonder48@hotmail.com> wrote in message
news:4bada569.0408220619.6ffd1c29@posting.google.com...
> Hi I'm still having some problems getting my head round this language.
> A couple of things don't seem to work for me. First I am trying to
> obtan a count of the number of words in a sting, so am using the split
> function with ' ', but how do i get it to take into account
> punctuation marks such as ',',',' etc?
>
> Also I am then trying to add contents of an array of strings to an
> arraylist, but only if the string isn't already there. I was using the
> arraylist.contains method, but it is still adding duplicates, and
> finally, I was trying to sort my arraylist, in alphabetical order
> using arraylist.sort(), but that doesn't seem to sort it fully, any
> ideas on where i am going wrong, i have posted my code below. One
> final question, before i add my strings of words to my arraylist, is
> it possible to capitalise the first letter in each word? Thanks.
>
public static void ParseFile(string input, string output) {
System.IO.TextReader r = System.IO.File.OpenText(@"C:\input.txt");
System.IO.TextWriter w = System.IO.File.CreateText(@"C:\output.txt");
string s = r. ReadToEnd();
ArrayList myList = new ArrayList();
int numOccur = 0;
string[] mySplit = s.Split(' ');
Console.WriteLine("Num of words is " + mySplit.Length);
for(int x = 0; x < mySplit.Length; x++) {
if (!myList.Contains(mySplit[x])) {
myList.Add(mySplit[x]);
}
}
myList.Sort();
foreach (string item in myList) {
w.Write(item + "\n");
}
r.Close();
w.Close();
}
- Next message: freddy: "RE: C# & ADO Book(s)"
- Previous message: Bob Grommes: "Re: Rich text box question."
- Next in thread: Bob Grommes: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Maybe reply: Bob Grommes: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Maybe reply: Jon Skeet [C# MVP]: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Maybe reply: Etienne Boucher: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Maybe reply: Jon Skeet [C# MVP]: "Re: Help ArrayLis won't store, and how to remove duplicates????"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|