Convert text to string array with separator - c#

I want to convert an string to string[] separate with ','.
string text = "1,2,5,6";
string[] textarray =?

Use this:
string text = "1,2,5,6";
string[] text_array =text.Split(new []{','});
To remove empty entries use:
string[] text_array =text.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);

string text = "1,2,5,6";
string[] textarray = text.split(',');

array=text.split(",");
it's simple enough.

Use below code.
string[] textArr = text.Split(",");

Related

How can I find a string between repeated strings in C#?

How can I find a string between repeated strings?
For example, if
string str = #"||AAA||BBB||CCC||";
how can I find all the strings(AAA, BBB, CCC) in order between repeated strings(||)?
Just use String.Split:
var str = #"||AAA||BBB||CCC||";
var splits = str.Split(new string[] {"||"}, StringSplitOptions.RemoveEmptyEntries);

How to call String.Split that takes string as separator?

I have following string value: AnnualFee[[ContactNeeYear that I want to split using separator : "[["
The MSDN topic String.Split says that such function exists, so i used following code :
oMatch.Groups[0].Value.Split('[[');
but it throws an error saying:
Can not implicitly convert string[] to string
so how to split string value with separator : "[["
Try below code, it has worked for me:
string abc = "AnnualFee[[ContactNeeYear";
string[] separator = { "[[" };
string[] splitedValues = abc.Split(separator , StringSplitOptions.None);
I hope it will help you.. :):)
string text= "AnnualFee[[ContactNeeYear";
string[] parts= Regex.Split(text, #"\[\[");
The result is:
AnnualFee
ContactNeeYear
You can use Regex.Split(text, pattern) for such purpose.
In a short way ,
string yourString = "AnnualFee[[ContactNeeYear";
string [] _split = yourString.Split(new string[] { "[[" }, StringSplitOptions.None);

Char Parsing newline (C#)

the problem is simple but I can't figure out a way to overcome this.My applciation recieve a string (stringID) which is a list of IDs, either separated ";"+new line or just newline like :
ID1;
ID2;
ID3;
or
ID1
ID2
ID3
What im trying is to get a table with all those IDs ;
I tried :
string[] tabID = stringID.Split(';', char.Parse(Environment.NewLine));
And
string[] tabID = stringID.Split(';', '\r\n'));
string[] tabID = stringID.Split(';','\n');
nothing worked, can anyone help me ? thanks a lot
use the class StringReader and its method ReadLine to read each line individually.
The newline property is a string that can be one or two characters long, so use strings when you split. Use the RemoveEmptyEntries option, otherwise you will get the empty strings that are between the semicolon and the newlines.
string[] tabID = stringID.Split(
new string[] { ";", Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries
);
This should work:
string [] split = words.Split(new Char [] {';','\n'});
There is an overload for Split which takes a char[] of many separators.
Try:
String test = "ID1;ID2;ID3;";
String[] testarr = test.Split(';');
Try this:
string[] tabID = stringID.Split(new Char[] { ';', '\n', '\r'},
StringSplitOptions.RemoveEmptyEntries);

split a string in to multiple strings

123\r\n456t\r\n789
How can i split the string above in to multiple strings based on the string text
.split has only over load that takes char :(
string.Split has supported an overload taking an array of string delimiters since .NET 2.0. For example:
string data = "123text456text789";
string[] delimiters = { "text" };
string[] pieces = data.Split(delimiters, StringSplitOptions.None);
use string.split("text"), hope it will help.
I believe you want to split 123, 456, 789 as you have \r\n after them.
Easiest way I see is
string textVal = "123\r\n456t\r\n789";
textVal = textVal.replace("\r", "").replace("\n",",");
string arrVal[] = textVal.split(',');
Now you have arrVal containing 123, 456, 789.
Happy coding
String.Split supports also an array of strings. In your case you can do:
string s = "123\r\n456t\r\n789";
string[] parts = s.Split(new string[] {"\r\n"}, StringSplitOptions.None);

How can I merge back a string I split?

I like how I can do string [] stringArray = sz.split('.');
but is there a way to merge them back together? (stringArray.Merge(".");)
string mergedString = String.Join(" ", stringArray);
String.Join
String.Join()
Suppose there is a phone number like:
string str = "^^^^^546^64767";
string resultString = String.Join("-", str.Split(new string[] { "^" }, StringSplitOptions.RemoveEmptyEntries));
So you can join like this.
string merge = String.Concat(stringArray);

Categories

Resources