• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

How to convert a string to a string[]

Hi,

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!

Thanks.

James.
0
JAMES
Asked:
JAMES
  • 2
1 Solution
 
Jase-CoderCommented:
Hi you can use the string.split() function for spliting an string. This function returns string []
0
 
Jase-CoderCommented:
there are many overloaded versions of this function. So, I recommend you take a look here to see which is best for your situation:

http://msdn2.microsoft.com/en-us/library/ms131450.aspx
0
 
WinterMuteUKCommented:
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.

Wint.
0
 
JAMESAuthor Commented:
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.

James.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now