C# Textbox string separation - c#

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.

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.

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.

string[] parts = myTextbox.Text.Split();

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

To get three different strings in an array, you can use String.Split()
string[] myStringArray = OriginalString.Split(" ".ToCharArray());

string[] words = Regex.Split(textBox.Text, #"\s+");

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

Try :
string data = TextBox1.Text;
var s1 = data.Split();
string a = s1[0].ToString();
string b= s1[1].ToString();

Related

Split a string on the Empty Character literal. Break apart a string's symbols into an array

i have the following string =
"00101010"
and i need the following array/list:
var.ElementAt(0) = "0"
var.ElementAt(1) = "0"
var.ElementAt(2) = "1"
var.ElementAt(3) = "0"
var.ElementAt(4) = "1"
var.ElementAt(5) = "0"
var.ElementAt(6) = "1"
var.ElementAt(7) = "0"
if i try string.split('') the compiler complains about a empty character literal.
Thanks for any help.
You can use Linq:
var chars = "00101010".Select(c => c.ToString()).ToArray();
This should do it:
string mys = "hello";
char[] thechars = mys.ToCharArray();
Use ToCharArray. But the more pertinent question is what you need to do with your result since string already implements IEnumerable<char> allowing you to iterate and do whatever you need with the characters in the string.
If you want to get a specific character in a string, just index the string; there is no need to convert it to an array of anything:
var s = "0123";
var c = s[0]; // -> '0'
If you really need strings and not chars, then Tim's answer will work.
There is a one line reg-ex solution that returns string[] instead of char[]:
var arr = Regex.Split(s, "(?<=.)(?=.)");
It'd be interesting if there's a one line non-Linq solution or a shorter correct reg-ex.
Note: I'd have posted under the other reg-ex answer but it seems to have been deleted due to missing # string prefix and it's limitation to only splitting digits.

How to indicate whitespaces while reading from a .txt file

I have a simple .txt file with X,Y-values in it. It is structured like this:
-25.7754 35.87
-22.1233 32.16
-20.361 30.75
etc.
I am able to read single lines or the whole text to the end, with objstream.ReadToEnd(); & objstream.ReadLine().
But here's my question how could I indicate when the String after the first value ends so I can save/parse it to float & proceed reading the value of the next string?
Here is the read functionality I have so far :)
StreamReader objStream = new StreamReader("C:blablabla\\Text.asc");
textBox1.Text = objStream.ReadLine();
Thanks in advance,
BC++
Use String.split()
As requested, an example :
string s = "there is a cat";
//
// Split string on spaces.
// ... This will separate all the words.
//
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
The output is :
there
is
a
cat
Look at the string.Split methods:
var line1 = objStream.ReadLine();
var lineParts = line1.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
textBox1.Text = lineParts[0];
textBox2.Text = lineParts[1];
Note the use of an overload that uses StringSplitOptions.RemoveEmptyEntries - the means that if you have multiple spaces in succession, the result will not contain empty entries.
If you really mean white-space and not space then you have to go this way:
string line = "-25.7754 35.87";
string[] values = line.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
The difference from the other answers in the splitting character. If this not defined then white-space characters are assumed to be the delimiters. In other words you will get the same result for
string line = "-25.7754\t35.87"; // tab instead of spaces.
You will have the flexibility to split correctly fixed length or tab delimited lines using the same code.

Extract substrings from a given string in C#

I have a string from which i want to extract a required string as :
"S101 Peter"
"S3282 Steve"
How to extract only the Names i.e. Peter and Steve from the above two strings. I worked out with Replace, Remove, TrimStart, IndexOf but couldn't find out? Please help...
You want SubString:
var name = theString.SubString(theString.IndexOf(' ') + 1);
String S = "S101 Peter";
String S1 = S.split(" ")[1];
If you can ensure the pattern of "XXXX YYYY", you could probably just split it at the whitespace:
string name = "S101 Peter".Split(' ')[1];
A simple way would be "S101 Peter".Split(' ')[1]
You can also do .Split(' ')[1]
string s = "S101 Peter";
string[] substrings = s.split(' ');
string result = substrings [1];

How can I split a word from within brackets using the Split method in C#?

How can I split a word from within brackets like:
(animal)
I need to take only the word "animal" using C# split.
If you only want to split on brackets this will do:
string test = "(duck)(monkey)";
string[] animals = test.Split(new [] {'(', ')'},
StringSplitOptions.RemoveEmptyEntries);
animals now contains { "duck", "monkey"}. For a single animal input (i.e. (animal)) just take animals[0] or evaluate directly:
string animal = test.Split(new [] {'(', ')'},
StringSplitOptions.RemoveEmptyEntries)[0];
The documentation for the String.Split method already gives you examples of how to do this. Just specify the brackets as the delimiter characters you want to split on:
string originalString = "(animal)";
string[] newString = originalString.Split(new char[] {'(', ')'});
Output:
{"", "animal", ""}
Are you sure you need to use split()?
If it is as simple as you stated wouldn't
string justWord = "(animal)".Replace("(","").Replace(")","")
be more efficient and clearer?
Only the trim is enough to do this
string originalString = "(animal)";
originalString = originalString.Trim('(',')');
Here is
string searchValues = "(duck)(monkey)";
var matches = Regex.Matches(searchValues, #"\w+");
var values = (from matche in matches.Cast<Match>() select matche.Value).ToList();

Splitting string with string C# .net 1.1.4322

How do I split a string with a string in C# .net 1.1.4322?
String example:
Key|Value|||Key|Value|||Key|Value|||Key|Value
need:
Key|Value
Key|Value
Key|Value
I cannot use the RegEx.Split because the separating character is the ||| and just get every character separately.
I cannot use the String.Split() overload as its not in .net 1.1
Example of Accepted solution:
using System.Text.RegularExpressions;
String[] values = Regex.Split(stringToSplit,"\\|\\|\\|");
What about using #"\|\|\|" in your Regex.Split call? That makes the | characters literal characters.
One workaround is replace and split:
string[] keyvalues = "key|value|||key|value".replace("|||", "~").split('~');
here is an example:
System.Collections.Hashtable table;
string[] items = somestring.split("|||");
foreach(string item in items)
{
string[] keyvalue = item.split("|");
table.add(keyvalue[0],keyvalue[1]);
}
string input = "Hi#*#Hello#*#i#*#Hate#*#My#*#......" ;
string[] delim = new string[] { "#*#" };
string[] results = input.split(delim , StringSplitOptions.None);

Categories