Home > Software engineering >  Delete a string variable from Another String Variable in C#
Delete a string variable from Another String Variable in C#

Time:09-19

I have a string variable operation_sequence. I'd like to remove another string variable job.Description from it.

For example, if I wanted to add job.Description to operation_sequence, I can do:

operation_sequence  = job.Description;

and this works. But if I wanted to remove job.Description from operation_sequence, the following code does not work:

operation_sequence -= job.Description;

What's the best way to remove job.Description from operation_sequence?

CodePudding user response:

You could easily use String.Replace():

String HelloWord = "Hello World!";
String NewWord= HelloWord.Replace("o","");

NewWord will be=Hell Wrld!

CodePudding user response:

We can't use -= or - for string. But we can implement it for our own string class.

Solution 1

public class MyString
{
    public string Value { get; private set; }

    public MyString(string value)
    {
        Value = value;
    }

    public static MyString operator  (MyString left, MyString right)
    {
        return new MyString(left.Value   right.Value);
    }

    public static MyString operator -(MyString left, MyString right)
    {
        if (string.IsNullOrEmpty(left.Value))
            return left;

        if (string.IsNullOrEmpty(right.Value))
            return left;

        if (left.Value.EndsWith(right.Value))
        {
            int startIndex = left.Value.Length - right.Value.Length;
            string result = left.Value.Substring(0, startIndex);
            return new MyString(result);
        }

        return left;
    }

    public static implicit operator string(MyString value) => value.Value;

    public static implicit operator MyString(string value) => new MyString(value);
}

As you know we can't overload -= and =(See this). Therefore I overloaded - and . Now we can use our class like this:

MyString s1 = "This is ";
MyString s2 = "just a test";

string s3 = s1   s2;    // s3 = "This is just a test"
string s4 = s3 - s2;    // s4 = "This is "
  • Because of public static implicit operator MyString(string value) => new MyString(value) we can have something like MyString s1 = "test". It implicitly converts string to MyString.

  • Because of public static implicit operator string(MyString value) => value.Value we can have something like string s3 = MyString("test"). It implicitly converts MyString to string.

  • In the - operator we checked if the left operand ends with the right one, we removed it.

Solution 2

And also we can simply use an extension method like this:
public static class StringExtension
{
    public static string MinusString(this string baseString, string minusString)
    {
        if (string.IsNullOrEmpty(baseString))
            return baseString;

        if (string.IsNullOrEmpty(minusString))
            return baseString;

        if (baseString.EndsWith(minusString))
        {
            int startIndex = baseString.Length - minusString.Length;
            string result = baseString.Substring(0, startIndex);
            return new MyString(result);
        }

        return baseString;
    }
}

and now we can use it like this:

string s = "This is just a test";
string s3 = s.MinusString("a test");   // s3 = "This is just "
s3 = s3.MinusString("just ");          // s3 = "This is "

CodePudding user response:

Solution suggested by Klaus Gütter worked for me, which is defining operation_sequence as a List and converting it to a string only after manipulation, using String.Join.

private string operation_sequence;

List<string> ops = new List<string>(3);

// Add item to List: 
ops.Add(job.Description);

// or Remove item from List: 
ops.Remove(job.Description);

//then update operation_sequence string with values from List<string>:
operation_sequence = String.Join(", ", ops);

  • Related