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);
Related
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);
Trim() Function Not Working Please Help its not trimming the white space.
string samplestring = "172-6573-4955";
string[] array = samplestring .Split('-');
string firstElem = array.First();
string restOfArray = string.Join(" ", array.Skip(1));
array[1] = restOfArray.Trim(); // providing value "6573 4955"
The scenario is I am splitting string and merging 2nd and last index into one but it's merging with white spaces.
Trim() removes whitespace from the front and back of a string, not in the middle. The whitespaces in the middle are being inserted by the " " provided as the parameter to Join() method. You could provide string.empty instead.
Trim is used to remove all leading and trailing white-space. And you want to trim the white space from middle. You may try like this:
string restOfArray = string.Join("", array.Skip(1));
or better:
string restOfArray = string.Join(string.Empty, array.Skip(1));
instead of
string restOfArray = string.Join(" ", array.Skip(1));
Trim is working as expected. Taken from String.Trim Method:
Removes all leading and trailing white-space characters from the
current String object.
The possible solutions for you are :
string restOfArray = string.Join(string.Empty, array.Skip(1));
Or
array[1] = restOfArray.Replace(" ", string.Empty).Trim();
To just split the first n-let from the rest use an additional array or you will end up with spurious data. And join with the correct separator you used in split(-):
string restOfArray = string.Join("-", array.Skip(1));
string[] result = new string[] { firstElem, restOfArray };
You don't need Trim().
You can simplify your code this way:
string[] splitCode(string code)
{
string[] segments;
segments = code.Split('-');
return new string[] { segments.First(), string.Join("-", segments.Skip(1)) };
}
Trim really isn't need here; you can simply split the string and join the parts you want:
string source = "172-6573-4955";
string[] splitter = new string[] {"-"};
string[] result;
result = source.Split(splitter, StringSplitOptions.None);
string final = (result[1] + result[2]);
Console.Write(final);
Result:
65734955
Try this:
string samplestring = "172-6573-4955";
string[] array = samplestring.Split(new char[] {'-'},2);
string result = array[1].Replace("-","");
Also, for your question about randomly putting spaces, can u pls explain "randomly" here.
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(",");
I have a string of type
ishan,training
I want to split the string after "," i.e i want the output as
training
NOTE: "," does not have a fixed index as the string value before "," is different at different times.
e.g ishant,marcela OR ishu,ponda OR amnarayan,mapusa etc...
From all the above strings i just need the part after ","
You can use String.Split:
string[] tokens = str.Split(',');
string last = tokens[tokens.Length - 1]
Or, a little simpler:
string last = str.Substring(str.LastIndexOf(',') + 1);
var arr = string.Split(",");
var result = arr[arr.length-1];
sourcestring.Substring(sourcestring.IndexOf(',')). You might want to check sourcestring.IndexOf(',') for -1 for strings without ,.
I know this question has already been answered, but you can use linq:
string str = "1,2,3,4,5";
str.Split(',').LastOrDefault();
Although there are several comments mentioning the issue of multiple commas being found, there doesn't seem to be any mention of the solution for that:
string input = "1,2,3,4,5";
if (input.IndexOf(',') > 0)
{
string afterFirstComma = input.Split(new char[] { ',' }, 2)[1];
}
This will make afterFirstComma equal to "2,3,4,5"
Use String.Split(",") assign the result in to a string array and use what you want.
Heres a VB version. I'm sure its easy to translate to C# though
Dim str as string = "ishan,training"
str = str.split(",")(1)
return str
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);