Re: Help ArrayLis won't store, and how to remove duplicates????

From: Bob Grommes (bob_at_bobgrommes.com)
Date: 08/22/04


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();
}



Relevant Pages

  • Re: ArrayList....how create it?help me....
    ... The other rows have the same format that is string double double double ... I have to store this file as it is. ... Don't do any parsing, don't create arraylists, or anything like that. ... some sort of complicated conversion. ...
    (comp.lang.java.programmer)
  • Searching ArrayList of Classes
    ... I've got arraylists of simple classes bound to controls. ... Private myCipDisplay As String ... Public ReadOnly Property CipDisplay() As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: ArrayList Question
    ... albeit one with the wrong type (should have been string not ... "C# doesn't support named indexers; and if it did, ... then Foo foo = new Foo; ... Unfortunately I have three of the arrayLists in this class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Exception error with XmlTextReader
    ... suggested (and I am also using ArrayLists as well: ... > which may be failing on some string value in your XML file. ... > to numeric conversions in exception handling blocks in case the string ... > Instead of a dynamic array, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Check to see a string or single character is a number or character??
    ... If it is ONLY a single character, you can use VB's IsNumeric function; ... Function IsDigitsOnly(Value As String) As Boolean ... Function IsNumberAs Boolean ... ' Get local setting for decimal point ...
    (microsoft.public.vb.general.discussion)