views:

264

answers:

7

Hi,

I have textbox in c#, contains two or three strings with white spaces. i want to store those strings seperately.please, suggest me any code. thanx.

+10  A: 
var complexValue = @"asdfasdfsdf asdfasd fffff
asdfasdfasdf";
var complexValues = complexValue.Split();

NOTICE:
.Split() is a pseudo-overload, as it gets compiled as .Split(new char[0]).
additionally msdn tells us:

If the separator parameter is null or contains no characters, white-space characters are assumed to be the delimiters. White-space characters are defined by the Unicode standard and return true if they are passed to the Char.IsWhiteSpace method.

Andreas Niedermair
".Split() is a pseudo-overload, as it gets compiled as .Split(new char[0]). " - Nice. I never saw that in the documentation! Something new every day!
David Stratton
well ... i, the same as you, saw this overload used by another person and suggested that this ain't a valid overload. then i digged deeper with reflector and ... eye-opener!
Andreas Niedermair
It's an effect of the `params` keyword.
Ben Voigt
+1 for .Split() overload explanation
Gage
@ben: wooow ... thank you!
Andreas Niedermair
+1  A: 

To get three different strings in an array, you can use String.Split()

string[] myStringArray = OriginalString.Split(" ".ToCharArray());
David Stratton
you can write just OriginalString.Split(' ');
Kikaimaru
+2  A: 

Calling String.Split() with no parameters will force the method to consume all whitespace and only return the separated strings:

var individualStrings = originalString.Split();
Justin Niessner
A: 
string[] words =  Regex.Split(textBox.Text, @"\s+");
Ani
Can anyone tell me why this was downvoted?
Ani
+3  A: 
string[] parts = myTextbox.Text.Split();
ibram
+6  A: 

Hi,

Firstly use this name space

using System.Text.RegularExpressions;

in your code

 string Message = "hi i am fine";
 string []Record=Regex.Split(Message.Trim(), " ");

Output is an array. I hope it works.

PrateekSaluja
your example really gives us an impression of how useful `.TrimStart()` and `.TrimEnd()` are... damn ... why not simply use `.Trim()`
Andreas Niedermair
oh .. yes ... -1, due to not correspond to every white-space...
Andreas Niedermair
Why would you want to use Regex.Split instead of String.Split??
Gage
We have too many ways to do this so i have chosen this one.
PrateekSaluja
I agree it will work with string [] a= temp.Trim().Split(' ');
PrateekSaluja
Regex'ing for a single space character is like hitting someone over the head with a tank. Sure it's possible, and it gets the job done, but a beer bottle would have been easier.
highphilosopher
A: 

Try this:

        string str = @"this is my string";
        string[] arr = str.Split(new char[] { char.Parse(" ") });
Polaris
hey, why do you use `char.Parse` ...? you could simply use `new char[] { ' ' }` instead
Andreas Niedermair