user9969
user9969

Reputation: 16040

Splitting by string with a separator with more than one char

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

Answers (6)

K Mehta
K Mehta

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

FishBasketGordo
FishBasketGordo

Reputation: 23122

string[] strs = 
    string.Split(new string[] { "~#" }, StringSplitOptions.RemoveEmptyEntries);

Upvotes: 5

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

use String.Split

string str = "leftSide~#righside";
str.Split(new [] {"~#"}, StringSplitOptions.None);

Upvotes: 3

Bodrick
Bodrick

Reputation: 342

var s = "leftSide~#righside";
var split = s.Split (new string [] { "~#" }, StringSplitOptions.None);

var myLeft = split [0];
var myRight = split [1];

Upvotes: 2

ar3
ar3

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

Akram Shahda
Akram Shahda

Reputation: 14771

String myLeft = value.Substring(0, value.IndexOf(seperator));
String myRight = value.Substring(value.IndexOf(seperator) + 1);

Upvotes: 0

Related Questions