1

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?

1
  • 3
    Doesn't sound like anything string.Replace() couldn't do. Commented Aug 8, 2011 at 16:33

8 Answers 8

4
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"
Sign up to request clarification or add additional context in comments.

10 Comments

Probably the most elegant solution
Why use var there? You know that both of those variables are Strings.
Overuse of var is dangerous. I'm not a fan of it for simple types as strings
No good reason, I just use var almost everywhere. The type system takes care of it and I'm okay with that. However, that's a style thing, and ideally shouldn't detract from the overall solution. At least I hope ;)
@Corey: One reason var 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.
|
1

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

If one of the other strings in the list contains (but is not equal to) "valueForDelete", it will be affected by this code.
Maybe try something like ";valueForDelete;", with another special case for if it's the first one in the string.
@Ken If I don't want to use if it wouldn't help
0

However 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

0

Use this Regex to find valueForDelete: (?<=;|^)valueForDelete(?=;|$)

const string Pattern = @"(?<=;|^)string3(?=;|$)";    
var s = "string1;string2;string3;string4;string5;";    
var res = Regex.Replace(s, Pattern, string.Empty);

Comments

0

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

I like the solution proposed by Travis. If it is a really big list list you might try my approach because it skips the ToArray. But good chance that approach proposed by Travis is still faster.
0

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

If one of the other strings in the list contains (but is not equal to) "valueForDelete", it will be affected by this code.
Also, this will result in an empty value; "string1;string2;string3;;stringn";
0

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 = ";";

2 Comments

If one of the other strings in the list ends with (but is not equal to) "string3;", it will be affected by this code.
If string3 would be the last one in the list this wouldn't work.
0

Why Can't you just do this?

        public static string Replace(string input)
        {
            input = input.Replace("valueToDelete;", "");
            return input ;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.