?
Solved

How to convert a string to a string[]

Posted on 2006-05-16
4
Medium Priority
?
259 Views
Last Modified: 2010-04-16
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
Comment
Question by:JAMES
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Jase-Coder earned 2000 total points
ID: 16688400
Hi you can use the string.split() function for spliting an string. This function returns string []
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16688415
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
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16688455
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
 

Author Comment

by:JAMES
ID: 16688457
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 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