I have what is probably a very simple question.
I want to do a classic String.Split(), but with a string, not a character. Like string.Split("word") and get back an array just like if I had done string.Split('x').
You can use String.Split(string[], StringSplitOptions options).
The code will look like:
var results = theString.Split(new[] {"word"}, StringSplitOptions.None);
There is an available function overload on string.Split but it takes an array and an enum.
string test = "1Test2";
string[] results = test.Split(new string[] { "Test" }, StringSplitOptions.None);
Will yield an array containing "1" and "2".
Related
C# code to get the string between two strings
example :
mystring = "aaa.xxx.b.ccc.12345"
Need c# code to get the second string "xxx" between two ".", always the second string ignore other strings between the "." What is the best way to get "xxx" out of "aaa.xxx.b.ccc.12345"
And the second set of string can be anything
eg:
"aaa.123.b.ccc.12345" "aaa.re.b.ccc.45" "eee.stt.b.ccc.ttt" "233.y.b.ccc.5"
We can use string.Split() get an array of all strings delimited by the parameter you pass it. For example:
var strings = mystring.Split('.');
// strings = {"aaa", "xxx", "b", "ccc", "12345"}
var str = strings[1];
// str = "xxx"
mystring.Split('.').Skip(1).FirstOrDefault();
We split at each '.' and we ignore the first then take the first one.
We need handling of nulls. if not just use First
You can you this:
string[] mystrings = mystring.Split('.');
string secondString = strings[1];
I wonder if it's possible to use split to divide a string with several parts that are separated with a comma, like this:
10,12-JUL-16,11,0
I just want the Second part, the 12-JUL-16 of string and not the rest?
Yes:
var result = str.Split(',')[1];
OR:
var result = str.Split(',').Skip(1).FirstOrDefault();
OR (Better performance - takes only first three portions of the split):
var result = str.Split(new []{ ',' }, 3).Skip(1).FirstOrDefault();
Use LINQ's Skip() and First() or FirstOrDefault() if you are not sure there is a second item:
string s = "10,12-JUL-16,11,0";
string second = s.Split(',').Skip(1).First();
Or if you are absolutely sure there is a second item, you could use the array accessor:
string second = s.Split(',')[1];
Yes, you can:
string[] parts = str.Split(',');
Then your second part is in parts[1].
or:
string secondPart = str.Split(',')[1];
or with Linq:
string secondPart = str.Split(',').Skip(1).FirstOrDefault();
if (secondPart != null)
{
...
}
else
{
...
}
Also you can use not only one symbol for string splitting, i.e.:
string secondPart = str.Split(new[] {',', '.', ';'})[1];
You could use String.Split, it has an overloaded method which accepts max no of splits.
var input = "10,12-JUL-16,11,0"; // input string.
input.Split(new char[]{','},3)[1]
Check the Demo
Here's a way though the rest have already mentioned it.
string input = "10,12-JUL-16,11,0";
string[] parts = input.Split(',');
Console.WriteLine(parts[1]);
Output:
12-JUL-16
Demo
I have a string like so 1500, 1500 and I am trying to split it into array like so:
string[] PretaxArray = Pretax.Split(", ");
but I get this error:
The best overloaded method match for 'string.Split(params char[])' has some invalid arguments
what am I doing wrong ?
String.Split has another overload
string Pretax = "1500, 1500";
string[] PretaxArray = Pretax.Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries);
You should try this:
string[] PretaxArray = Pretax.Split(',');
In the Split method we usually pass a character or an array of characters and not a string.
Actually, in order to be more precise, you could pass an array of strings. But this is not your case - I assume that from the code you have posted.
You could take a look here, where all the overloaded versions of Split method are aggregated.
When i program with C#,using Split function:
string[] singleStr=str.Split(';');
the str is column111.dwg&186&0;
Why the singleStr.Length=2? Why give a array althrough the array is null?
I'm not sure what desStr looks like but sounds like you need to use StringSplitOptions.RemoveEmptyEntries
The return value does not include array elements that contain an empty
string
string str = "column111.dwg&186&0;";
string[] singleStr = str.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in singleStr)
{
Console.WriteLine(item);
}
Output will be only;
column111.dwg&186&0
Here a demonstration
If we don't use StringSplitOptions.RemoveEmptyEntries in this case, singleStr array has 2 items; column111.dwg&186&0 and ""
If you dont want to have empty entries, use this construction:
string[] singleStr=str.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries);
Basically, it's correct to have two items in your array as you do have a semicolon in your string.
One of the prototypes of Split method allow you to set the SplitStringOptions to RemoveEmptyEntries
Eg:
var parts = yourString.Split( new []{';'}, SplitStringOptions.RemoveEmptyEntries);
Use this:
string[] singleStr=str.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries);
it will remove null's and empty strings like whitespaces and others
Answer to your question:
array length is 2 because Split sees: column111.dwg&186&0; and do split on ; and gets:
column111.dwg&186&0 and after ; it has null string only
If you wish to ignore empty entries, try using
String.Split Method (Char[], StringSplitOptions)
Returns a string array that contains the substrings in this string
that are delimited by elements of a specified Unicode character array.
A parameter specifies whether to return empty array elements.
StringSplitOptions Enumeration
RemoveEmptyEntries:
The return value does not include array elements that contain an empty
string
This is because it splits the string everywhere it finds a ";" in the string and that results in an empty entry because your ";" is at the end of the string.
You can use following call to remove emtpy entries:
str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
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);