2

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.

0

8 Answers 8

11
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.

Sign up to request clarification or add additional context in comments.

3 Comments

".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!
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!
It's an effect of the params keyword.
6

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.

4 Comments

your example really gives us an impression of how useful .TrimStart() and .TrimEnd() are... damn ... why not simply use .Trim()
Why would you want to use Regex.Split instead of String.Split??
I agree it will work with string [] a= temp.Trim().Split(' ');
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.
3
string[] parts = myTextbox.Text.Split();

Comments

2

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

var individualStrings = originalString.Split();

Comments

1

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

string[] myStringArray = OriginalString.Split(" ".ToCharArray());

1 Comment

you can write just OriginalString.Split(' ');
0
string[] words =  Regex.Split(textBox.Text, @"\s+");

Comments

0

Try this:

        string str = @"this is my string";
        string[] arr = str.Split(new char[] { char.Parse(" ") });

1 Comment

hey, why do you use char.Parse ...? you could simply use new char[] { ' ' } instead
0

Try :

    string data = TextBox1.Text;
    var s1 = data.Split();

    string a = s1[0].ToString();
    string  b= s1[1].ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.