How to convert a string to a string[]

Posted on 2006-05-16
Last Modified: 2010-04-16

I have a stream with standand ascii CR/LF delimiting new lines.  I would like to insert this into a string array. This is my current method utilising the formatting of a richtextbox but I would like to be able to achieve this without the overhead of the richtextbox control - is there a better way than doing a loop and adding each line individually to the array?

      byte[] b = (byte[])reader.GetValue(0);
      RichTextBox rtb = new RichTextBox();
      rtb.Text = System.Text.Encoding.ASCII.GetString(b,0,b.Length);
      Macro = rtb.Lines;

Would appreciate your thoughts!


Question by:JAMES
    LVL 11

    Accepted Solution

    Hi you can use the string.split() function for spliting an string. This function returns string []
    LVL 11

    Expert Comment

    there are many overloaded versions of this function. So, I recommend you take a look here to see which is best for your situation:
    LVL 9

    Expert Comment

    Hi James,

    Just for reference, if you want to split on CRLF you'll need this:

      string myString = "Hullo\r\nWorld";
      string[] myStringSplit = myString.Split("\r\n".ToCharArray());

    alternatively you could use 'Environment.NewLine' instead of \r\n.

    This is just to give a code example, it's the same as Jase-Coders answer.


    Author Comment

    Cool, thanks.

    This is my solution now :-

          byte[] b = (byte[])reader.GetValue(0);
          string strText = System.Text.Encoding.ASCII.GetString(b,0,b.Length);
          string delimStr = "\n";
          char [] delimiter = delimStr.ToCharArray();
          Macro = strText.Split(delimiter);

    Many thanks.


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Contant UI messages in .NET 1 20
    Global Keyboard Hooks Blocked 4 37
    Refresh page ASP.NET C# 3 39
    What is MicroStrategy.NET? 2 23
    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now