CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2011
    Posts
    1

    Splitting strings by line

    How can I split a string by lines... For example

    Line1
    Line2
    Line3



    I want to split it into a list

    so lines[1] = "line1"


    see? So how could I do this?

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Splitting strings by line

    Code:
    string longString = //Whatever;
    string[] lines = allStringsTogether.Split("\r\n", StringSplitOptions.None);
    Or for maximum portability

    Code:
    string longString = //Whatever;
    string[] lines = allStringsTogether.Replace("\r\n","\n").Split('\n');
    N.B. that this scheme will give you a zero-indexed array:

    lines[0] = "line1"
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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