Results 1 to 5 of 5

Thread: simple -> how can I convert a multiline string to an array

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    simple -> how can I convert a multiline string to an array

    I have a string with a bunch of "new line" characters. I want each line to be an element of a string array. I dunno what I'm doing wrong but it doesnt work too well with String.Split

    I'm doign this:
    String.Split (Environment.NewLine.ToCharArray());

    every other element of the array appears to be blank (""). what's the right way?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I did this and it looks it's working .....
    PHP Code:
    System.IO.StreamReader sr=new System.IO.StreamReader("c:\\a.txt");
                
    string[] ss =sr.ReadToEnd().Split(Environment.NewLine.ToCharArray());
                
                foreach (
    string s in ss )
                {
                    if (
    !="")MessageBox.Show(s);
                }
            } 

  3. #3
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    Try:

    string[] keywordArray = keywordString.Split('\n');

    DJ

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    Also you must remember that if there is more than one line return without any text they will still be split - which might be why some elements in the array are blank.

    DJ

  5. #5
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    Evironment.Newline is equal to "\r\n" (assuming windows here), so your splitting by each element in the char array {'\r', '\n'}. So when you get a file like this:

    line 1 \r\n
    line 2 \r\n

    you end up with the following:
    line 1 (ends at \r)
    null string (between \r and \n)
    line 2 (ends at \r)
    null string (between \r and \n)

    Does that make sense?
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

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