Remove strings between double quotes

How many times you have to bullet proof your application against some bad or junk data ? Sometimes we might not have anticipated some scenario until quite a while. And this can quite possible if your application relies on some other program ( data from web service, some sort of incoming XML or something like that )

Well LINQ is your friend !

Just a heads up :

Going forward I shall try to maintain all string manipulation related posts ALSO under ‘string-manipulation‘ tag, just if that helps you drill down the content better. Cheers ! 😉

// code snippet

string s = "kf  khg ddkfhg dfgh ldfg ss=\"XXXX\" dfgdfkgjhdkfgh ss=\";XXX YYY Z\" kjdhkfhg difhgkdhgfk dhkgfh dkfghkdhgf ss=\"XX Y ZZZ\" fghfgh\"";
var v = s.Split('"').Where((ss, n) => n % 2 == 0);
string stripped = string.Join("", v);

// end

Output :

kf  khg ddkfhg dfgh ldfg ss= dfgdfkgjhdkfgh ss= kjdhkfhg difhgkdhgfk dhkgfh dkfghkdhgf ss= fghfgh

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s