C # Split by String [] Delimiter is only an exact word

advertisements

I have string where its value should be split by string delimiter. For example my delimiter is "at", I want to split only the string value by exact "at" keyword. Below is my sample string value

 var sampleStr= "at Metadata at quota at what at batter";

If I use the code below, the words with "at" in them are also split.

var result= sampleStr.Split(new string[] { "at" }, StringSplitOptions.None);

The result that I want is an array that if combined will be "Metadata quota what batter".

Please help.


 splitted = Regex.Split(text,@"\bat\s*\b");

\b denotes any word-boundary. \s* will match the whitespace characters after "at".

splitted : string [] = [|""; "Metadata "; "quota "; "what "; "batter"|]

If you don't need a Empty Space then try like below...

List<string> splitted = Regex.Split(phrase, @"\bat\s*\b",StringSplitOptions.RemoveEmptyEntries);

splitted : string [] = [| "Metadata "; "quota "; "what "; "batter"|]