Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to split a string by another string?

Tags:

c#

I have a string in following format

"TestString 1 <^> TestString 2 <^> Test String3

Which i want to split by "<^>" string.

Using following statement it gives the output i want

"TestString 1 <^> TestString 2 <^> Test String3"
 .Split("<^>".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

But if my string contains "<" , ">" or "^" anywhere in the text then above split statement will consider that as well

Any idea how to split only for "<^>" string ?

like image 578
Nikhil Vaghela Avatar asked Dec 03 '22 13:12

Nikhil Vaghela


1 Answers

By using ToCharArray you are saying "split on any of these characters"; to split on the sequence "<^>" you must use the overload that accepts a string[]:

string[] parts = yourValue.Split(new string[]{"<^>"}, StringSplitOptions.None);

Or in C# 3:

string[] parts = yourValue.Split(new[]{"<^>"}, StringSplitOptions.None);
like image 71
Marc Gravell Avatar answered Dec 21 '22 00:12

Marc Gravell