I have a string in this format "string1;string2;string3;...;stringn"
; as a delimiter
I need to delete some string value of which I know, say valueForDelete
I use string.Split(';') method to find my value, delete it and then create a new string without deleted value.
I'm wondering is it possible to make this process easy with regex?
-
3Doesn't sound like anything string.Replace() couldn't do.Hans Passant– Hans Passant2011-08-08 16:33:34 +00:00Commented Aug 8, 2011 at 16:33
8 Answers
var values = "string1;string2;string3;string4";
var cleanedValues = String.Join(";",
values.Split(';')
.Where(x => x != "string3")
.ToArray())
Regex is a useful tool, and could be used for this, but often hard to maintain. Something like the above would likely provide an easier to maintain solution. Regex can be tricky if your string also contain regex characters. As a bonus, this is easy to extend.
static string CleanItUp(string values, params string[] removeMe)
{
return String.Join(";",
values.Split(';')
.Except(removeMe)
.ToArray());
}
Used like.
var retString = CleanItUp("string1;string2;string3;", "string1", "string2");
// returns "string3"
10 Comments
var is dangerous. I'm not a fan of it for simple types as stringsvar was invented exactly for when it's obvious what type the variable should be. In this case, it's obvious, so var is a good thing.Why not just:
string s = "string1;string2;string3;valueForDelete;string4"
s = s.Replace("valueForDelete;", string.Empty).Replace("valueForDelete", string.Empty);
The second replace is for if the value is the last one.
3 Comments
";valueForDelete;", with another special case for if it's the first one in the string.if it wouldn't helpHowever possible with RegEx, using Split and Join will be your easiest, most functional choice. If you had a more complex method of choosing what Strings to delete, you could use the Where clause.
String input = "string1;string2;string3";
String valueForDelete = "string2";
String[] parts = input.Split(';');
var allowed = parts.Where(str => !str.Equals(valueForDelete));
String output = String.Join(";", allowed);
If you are simply removing an exact value than String.Replace would be better.
Comments
Regex will do that but not sure that for what you are asking it would be faster. .Split is fast. If you were spitting on something more complex then you would have to use regex. I assume you are using StringBuilder to build the new string? String += is slow. When you new the StringBuilder make it the size you expect.
1 Comment
For Replacement ensuring no other data is affected (using LINQ):
string test = "string1;string2;string3;valueForDelete;stringn";
test = String.Join(";", test.Split(';').Where(s => s != "valueForDelete"));
For simple replacement (using String.Replace()):
string test = "string1;string2;string3;valueForDelete;stringn";
test = test.Replace("valueForDelete;", "");
2 Comments
Couldn't you just say
var myString = "string1;string2;string3;string4;string5;";
myString = myString.Replace("string3;", "");
The result would be a myString with the value "string1;string2;string4;string5;"
EDIT: Created as a regex
public static Regex regex = new Regex("(?:^|;)string3(;|$)",
RegexOptions.CultureInvariant | RegexOptions.Compiled
);
myString = regex.Replace(myString, ";");
...only flaw I see at the moment is if myString = "string3"; it results in myString = ";";