Reputation: 422
I am testing to cut the strings via C#, but I am not getting the results correctly. It is still showing the full text exactString.
String exactString = ABC@@^^@@DEF
char[] Delimiter = { '@', '@', '^', '^', '@', '@' };
string getText1 = exactString.TrimEnd(Delimiter);
string getText2 = exactString.TrimStart(Delimiter);
MessageBox.Show(getText1);
MessageBox.Show(getText2);
OUTPUT:
ABC@@^^@@DEF
for both getText1 and getText2.
Correct OUTPUT should be ABC for getText1 and DEF for getText2.
How do I fix it? Thanks.
Upvotes: 0
Views: 2046
Reputation: 3439
you can use String.Split Method
String exactString = "ABC@@^^@@DEF";
string[] splits = exactString.Split(new string[]{"@@^^@@"}, StringSplitOptions.None);
string getText1 = splits[0];
string getText2 = splits[1];
MessageBox.Show(getText1);
MessageBox.Show(getText2);
Upvotes: 1
Reputation: 12608
You are looking for String.Replace, not Trim.
char[] Delimiter = { '@', '^' };
string getText1 = exactString.Replace(Delimiter,'');
Trim only removes the characters at the beginning, Replace looks through the whole string.
You can split strings up in 2 pieces using the (conveniently named) String.Split method.
char[] Delimiter = { '@', '^' };
string[] text = exactString.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries);
//text[0] = "ABC", text[1] = "DEF
Upvotes: 1
Reputation: 172230
You want to split your string, not trim it. Thus, the correct method to use is String.Split
:
String exactString = "ABC@@^^@@DEF";
var result = exactString.Split(new string[] {"@@^^@@"}, StringSplitOptions.None);
Console.WriteLine(result[0]); // outputs ABC
Console.WriteLine(result[1]); // outputs DEF
Upvotes: 7