CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2012
    Posts
    18

    Question Problem:How to use the string array after using the Split() method?

    I have a hashtable which stores all the lines of a paragraph at different indices.
    Next,I want to split each line in the hashtable based on comma.

    My Code:
    foreach(DictionaryEntry entry in h1)
    {
    string val = (string)entry.Value;
    string[] synonyms=val.Split(',');
    string variable=synonyms[0];
    /*works fine till here*/
    int i=1
    while(true)
    {
    Console.WriteLine(synonyms[i]);
    i++;
    }
    }

    What I want to do is once the line is split based on comma, each part is saved as an array..
    In a string called variable, I am assigning part0..
    Then I want to use a loop for parsing the parts from index 1 onwards..
    Whenever I try to do that I get unhandled exceptions...

  2. #2
    Join Date
    Jan 2012
    Posts
    18

    Re: Problem:How to use the string array after using the Split() method?

    Quote Originally Posted by Pearls View Post
    I have a hashtable which stores all the lines of a paragraph at different indices.
    Next,I want to split each line in the hashtable based on comma.

    My Code:[code]
    foreach(DictionaryEntry entry in h1)
    {
    string val = (string)entry.Value;
    string[] synonyms=val.Split(',');
    string variable=synonyms[0];
    /*works fine till here*/
    int i=1
    while(true)
    {
    Console.WriteLine(synonyms[i]);
    i++;
    }
    }

    What I want to do is once the line is split based on comma, each part is saved as an array..
    In a string called variable, I am assigning part0..
    Then I want to use a loop for parsing the parts from index 1 onwards..
    Whenever I try to do that I get unhandled exceptions...
    My 1st time..So having a little trouble in posting..

  3. #3

    Re: Problem:How to use the string array after using the Split() method?

    This index is zero based, if there is no 1, you will cause an exception.

    You might also wish to check length as well: if synonyms[i].Length > 0 ...
    Last edited by ZOverLord; January 14th, 2012 at 10:41 AM.

  4. #4
    Join Date
    Jan 2012
    Posts
    18

    Re: Problem:How to use the string array after using the Split() method?

    ok..Thankx ZOverLord..I will do that as well..

    I will explain with an example..
    I have a line "Andy,Ram,Seth")

    After splitting,I get
    synonym[0]=Andy
    synonym[1]=Ram
    synonym[2]=Seth

    I assign Andy to variable..
    Next,I want to do the processing from index 1 i.e only for Ram and Seth..

  5. #5

    Re: Problem:How to use the string array after using the Split() method?

    Quote Originally Posted by Pearls View Post
    ok..Thankx ZOverLord..I will do that as well..

    I will explain with an example..
    I have a line "Andy,Ram,Seth")

    After splitting,I get
    synonym[0]=Andy
    synonym[1]=Ram
    synonym[2]=Seth

    I assign Andy to variable..
    Next,I want to do the processing from index 1 i.e only for Ram and Seth..

    Code:
                            string val = "Andy,Ram,Seth";
                            string variable = "";
                            String[] synonyms = val.Split(new Char[] { ',' });
    
                            if (synonyms.Length > 0)
                                variable = synonyms[0];
                            else
                                variable = "";
    
                            if ((synonyms.Length > 1)
                                && (variable.Length > 0))
                                for (int i = 1; i < (synonyms.Length); i++)
                                {
                                    // Your code here
                                    if (synonyms[i].Length > 0)
                                        MessageBox.Show(synonyms[i]);
                                }
                            else
                            {
                                // Was empty
                            }
    IMHO, I would never assume a random string or a string array produced by a string split from a random string has any length, when doing processing like this, while some might say it is "OverKill" to check, **** Happens!
    Last edited by ZOverLord; January 14th, 2012 at 01:06 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured