-1

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?

Robabeh Ghasemi
  • 422
  • 1
  • 3
  • 11
Baslki
  • 63
  • 1
  • 1
  • 9
  • Replace with empty string? – ProgrammingLlama Sep 18 '22 at 04:40
  • 1
    Do you want to remove all occurrences of `job.Description` from `operation_sequence` or only the first, or only the last? – Klaus Gütter Sep 18 '22 at 05:22
  • @KlausGütter All occurrences. For example, operation_sequence could be equal to "Op1,Op2,Op3" and job description = "Op2". I want to delete "Op2," from operation_sequence. – Baslki Sep 18 '22 at 05:28
  • 1
    When you are using `operation_sequence` like this, you better define it as `List`) and convert to a string only when needed using `string.Join(",", operation_sequence)` – Klaus Gütter Sep 18 '22 at 05:30
  • 1
    Does this answer your question? [C# String Replace safely](https://stackoverflow.com/questions/41036227/c-sharp-string-replace-safely) – IteratioN7T Sep 18 '22 at 06:51

3 Answers3

1

You could easily use String.Replace():

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

NewWord will be=Hell Wrld!

H H
  • 263,252
  • 30
  • 330
  • 514
Shibu
  • 102
  • 9
  • Please test your code before posting. Ideally you should demonstrate what the OP needs to do to get their code working. – Enigmativity Sep 18 '22 at 04:45
  • 1
    Using `string.Empty` is still "hardcoding". The main purpose of it is to signify to other developers that the empty string was deliberate rather than just using an empty string literal where someone may think you just forgot to add text. – Jesse Sep 18 '22 at 04:46
  • @Enigmativity,Please have a look at this link https://dotnetfiddle.net/7Aq6i9 – Shibu Sep 18 '22 at 05:20
  • 1
    @Shibu - your original code was `HelloWord.replace("o",string.Empty)` which doesn't compile. – Enigmativity Sep 18 '22 at 05:33
0

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 "
Saeid Amini
  • 1,313
  • 5
  • 16
  • 26
0

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);
Baslki
  • 63
  • 1
  • 1
  • 9