Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys,

i have a 'foreach' statement that is a little like this:

C#
foreach (string a in text.Split('\n'))
{
}


i need to load all the tokens that has been split up into a list called tokens. is there any way i can do this without too much code? please help!
Posted
Comments
Sergey Alexandrovich Kryukov 6-Jul-12 1:18am    
What do you mean by "load"? Where do you see "too much code"?
--SA

The string.SPlit returns an array of strings.

so you can have something like
C#
string[] items = text.Split('\n');

and further to make it type safe(if you know the number of items that will be coming)
C#
enum ITEMS
{
 item0 = 0,
 tem1 = 1
}

and access it as
C#
var variable = items[ITEMS.item0];

Note: I am not sure whether this will give you less code but this is an alternate way of doing it.
 
Share this answer
 
:doh:

Try:
C#
string someText = "My name is \n Sandeep Mewara. \n Thanks for asking.";
string[] textValues = someText.Split('\n');
List<string> tokens = new List<string>();
tokens = textValues.ToList();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900