textbox text to array with C# - c#

Can anyone help with a little code i want to make array which first index will have first word of textbox text:
array[0] first word of text
array[1] second word of text
can anyone help me?

string str = "Hello Word Hello" ;
var strarray = str.Split(' ') ;
You can replace the str with TextBox.Text...

Use the Split method of the string type.
It will split your string by a character specification to a string array (string[]).
For example:
textBox1.Text = "The world is full of fools";
string[] words = textBox1.Text.Split(' ');
foreach(string word in words)
{
//iterate your words here
}

If they are seperated by spaces :
var MyArray = MyTextBox.Text.Split(' ');

There's a very simple way of doing this:
string text = "some text sample";
List<string> ltext = text.Split(' ').ToList();

U can use the split method,it gets a string array back,you need to pass a char array with the characters to split upon:
string[] str = textBox1.Text.Split(new char[] { ' ', ',', ':', ';', '.' });

use .Split() method like this:
var array = textboxText.Split(' ');

Related

How to break a single string into an array of strings?

I have a program in which I read from a string that's formatted to have a specific look.
I need the numbers which are separated by a comma (e.g. "A,B,D,R0,34,CDF"->"A","B","D","R0", "34", "CDF").
There are commas between letters that much is guaranteed
I have tried to do (note: "variables" is a 2D char array, newInput is a string which is a method argumend, k and j are declared and defined as 0)
for(int i=0; i<=newInput.Length; i++){
while(Char.IsLetter(newInput[i])){
variables[k,j]=(char)newInput[i];
i++;
k++;
}
k=0;j++;
}
with multi-dimensional character arrays.
Is there a way for this to be done with strings? Because char arrays conflict with many parts of the program where this method has already been implemented
Simple. Just use the Split method:
var input = "A,B,D,R0,34,CDF";
var output = input.Split(','); // [ "A", "B", "D", "R0", "34", "CDF" ]
Try this:
string MyString="A,B,D,R0,34,CDF";
string[] Parts = MyString.Split(',');
And use them like:
Parts[0];//A
Parts[1];//B
Parts[2];//D
Parts[3];//R0
Parts[4];//34
Parts[5];//CDF
If you want to know more about Split function. Read this.
if you want to Split the string on line breaks use this
string str = "mouse\r\dog\r\cat\r\person\r\pig";
string[] lines = Regex.Split(str, "\r\n");
and if you want to split with chars , as input ,Split takes array of chars
char[] myChars = {':', ',', '.', '\u', ' ' };
string myString = "jack:tom kasra\unikoo car,pencil ball";
string[] myWords = myString.Split(myChars);

How to break a string at each comma?

Hi guys I have a problem at hand that I can't seem to figure out, I have a string (C#) which looks like this:
string tags = "cars, motor, wheels, parts, windshield";
I need to break this string at every comma and get each word assign to a new string by itself like:
string individual_tag = "car";
I know I have to do some kind of loop here but I'm not really sure how to approach this, any help will be really appreciate it.
No loop needed. Just a call to Split():
var individualStrings = tags.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
You can use one of String.Split methods
Split Method (Char[])
Split Method (Char[], StringSplitOptions)
Split Method (String[], StringSplitOptions)
let's try second option:
I'm giving , and space as split chars then on each those character occurrence input string will be split, but there can be empty strings in the results. we can remove them using StringSplitOptions.RemoveEmptyEntries parameter.
string[] tagArray = tags.Split(new char[]{',', ' '},
StringSplitOptions.RemoveEmptyEntries);
OR
string[] tagArray = s.Split(", ".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries);
you can access each tag by:
foreach (var t in tagArray )
{
lblTags.Text = lblTags.Text + " " + t; // update lable with tag values
//System.Diagnostics.Debug.WriteLine(t); // this result can be see on your VS out put window
}
make use of Split function will do your task...
string[] s = tags.Split(',');
or
String.Split Method (Char[], StringSplitOptions)
char[] charSeparators = new char[] {',',' '};
string[] words = tags.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
string[] words = tags.Split(',');
You are looking for the C# split() function.
string[] tags = tags.Split(',');
Edit:
string[] tag = tags.Trim().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
You should definitely use the form supplied by Justin Niessner. There were two key differences that may be helpful depending on the input you receive:
You had spaces after your ,s so it would be best to split on ", "
StringSplitOptions.RemoveEmptyEntries will remove the empty entry that is possible in the case that you have a trailing comma.
Program that splits on spaces [C#]
using System;
class Program
{
static void Main()
{
string s = "there, is, a, cat";
string[] words = s.Split(", ".ToCharArray());
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}
Output
there
is
a
cat
Reference

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();

Get a file/directory path from a string that contains other data

How would I get the directory and filename from the following string using C#:
string test = "test#test.com, snap555.jpg, c:\users\test\desktop\snap555.jpg";
I only want to be able to get the "c:\users\test\desktop\snap555.jpg" from the string and turn it into another string.
The characters before the "," will always be different and different lengths such as: "bob#bob.com, x.txt, c:\users\test\desktop\x.txt"
What is the easiest way to do this in c#?
Thanks.
t
If the comma delimiter does not appear in the first part, you can use:
string pathName = test.Split(',')[2];
If that space after the comma is a problem and assuming that the first part never has spaces, you can use:
char[] delims = new char[] { ',', ' '};
string pathName = test.Split(delims, StringSplitOptions.RemoveEmptyEntries)[2];
This example assumes you always want the third item, as mentioned in your question. Otherwise, change the 2 in the examples above to the correct index of the item you want. For example, if it should always be the last item, you can do:
char[] delims = new char[] { ',', ' '};
string[] items = test.Split(delims, StringSplitOptions.RemoveEmptyEntries);
string pathName = items[items.Length-1];
If there's always going to be a comma there you can use
string test = "snap555.jpg, c:\users\test\desktop\snap555.jpg";
string[] parts = test.Split(',');
Then get whatever you need from the parts array.

C# Textbox string separation

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();

Categories