Reputation: 16040
Suppose i have a string separator eg "~#" and have a string like "leftSide~#righside" How do you get you leftside and rightside without separator?
string myLeft=?;
string myRight=?
How do you do it? thanks
Upvotes: 2
Views: 741
Reputation: 10533
string[] splitResults = myString.Split(new [] {"~#"}, StringSplitOptions.None);
And if you want to make sure you get at most 2 substrings (left and right), use:
int maxResults = 2;
string[] splitResults =
myString.Split(new [] {"~#"}, maxResults, StringSplitOptions.None)
Upvotes: 7
Reputation: 23122
string[] strs =
string.Split(new string[] { "~#" }, StringSplitOptions.RemoveEmptyEntries);
Upvotes: 5
Reputation: 50712
use String.Split
string str = "leftSide~#righside";
str.Split(new [] {"~#"}, StringSplitOptions.None);
Upvotes: 3
Reputation: 342
var s = "leftSide~#righside";
var split = s.Split (new string [] { "~#" }, StringSplitOptions.None);
var myLeft = split [0];
var myRight = split [1];
Upvotes: 2
Reputation: 4063
the split function has an overload that accepts an array of strings instead of chars...
string s = "leftSide~#righside";
string[] ss = s.Split(new string[] {"~#"}, StringSplitOptions.None);
Upvotes: 2
Reputation: 14771
String myLeft = value.Substring(0, value.IndexOf(seperator));
String myRight = value.Substring(value.IndexOf(seperator) + 1);
Upvotes: 0