Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a part of string effectively

Tags:

Have a string like A=B&C=D&E=F, how to remove C=D part and get the string like A=B&E=F?

like image 476
user496949 Avatar asked Feb 17 '11 10:02

user496949


1 Answers

Either just replace it away:

input.Replace("&C=D", ""); 

or use one of the solutions form your previous question, remove it from the data structure and join it back together.

Using my code:

var input = "A=B&C=D&E=F"; var output = input                 .Split(new string[] {"&"}, StringSplitOptions.RemoveEmptyEntries)                 .Select(s => s.Split('=', 2))                 .ToDictionary(d => d[0], d => d[1]);  output.Remove("C"); output.Select(kvp => kvp.Key + "=" + kvp.Value)       .Aggregate("", (s, t) => s + t + "&").TrimRight("&"); 
like image 138
Femaref Avatar answered Oct 14 '22 21:10

Femaref