2

This is one of the questions. Basically im stuck.

Write a method that removes all occurrences of a string from another string:

    [Test]
    public void TestExercise12()
    {
        Programmeren2Tests.Chapter12Test.TestExercise12(Exercise12);
    }

    public static string Exercise12(string strToRemove, string str)
    {
        strToRemove.Replace("str", "x");
        return str;

    }

What I did here obviously didn't work, but I have no idea where to search on how to do this.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
Dell
  • 21
  • 2
  • [`String.Replace()`](https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx) does not modify your string, it *returns* a brand new string with the replacements made. To "remove" a matched string, you can replace it with `string.Empty` which is equivalent to `""`. Your code should simply be, `return str.Replace(strToRemove, string.Empty);`. Edit: actually, reading the MSDN page I linked you can either use `null` or `string.Empty` as the replacement because, "If newValue is null, all occurrences of oldValue are removed.". – Quantic Nov 23 '16 at 18:39
  • Also, is your homework meant to be to simply *utilize* the `Replace` method, or are you expected to actually implement the underlying algorithm? – Dai Nov 23 '16 at 18:40
  • 1
    I did want to note though, don't just say "it obviously didn't work", it might not be so obvious at first glance (even if in this case it was). Describe what actually happens and is returned – Dispersia Nov 23 '16 at 18:43
  • Thanks it worked! The thing is I don't know where to find how these things are done. I know what has been done here but I can't come up with it myself. Thanks anyways! – Dell Nov 23 '16 at 18:47
  • You could start with "string replace doesn't work" as a search term in your favorite search engine... – spender Nov 23 '16 at 18:56
  • Since your school assignment is to _Write a method that removes all occurrences of a string from another string_ I'm **guessing** your teacher may want you to write a method that removes all occurences of a string, and not want you to use a method that's already available as you are doing with `.Replace()`. If you ask your question again, since this one is closed, I'll have a solution to offer. – blaze_125 Nov 23 '16 at 19:11
  • Here is what I think you were after initially, [https://dotnetfiddle.net/37M3sx](https://dotnetfiddle.net/37M3sx) – blaze_125 Nov 23 '16 at 19:30

3 Answers3

5

Strings are immutable in C#/.NET, so foo.Replace will not update the value of foo, instead it will return a new value:

String foo         = "abc";
String fooWithoutB = foo.Replace( "b", "" );

Assert.AreEqual( "abc", foo         );
Assert.AreEqual( "ac" , fooWithoutB );

A few other notes:

  • Avoid "Hungarian Notation" (e.g. the str prefix to your variable names). This is discouraged.
  • Use meaningful names. str doesn't tell me anything about what it's intended for. Consider a signature like RemoveAllOcurrences(String needle, String haystack) or (String ofThis, String fromThis) instead.
  • Your original method returned the original second parameter unchanged. You could have identified this bug if you used your IDE's step-through debugger.
Dai
  • 141,631
  • 28
  • 261
  • 374
1

First off, the primary place you should look when running into something like this is the documentation found on MSDN. (Link for String.Replace)

Your fist mistake though, is obvious. You are using string constants instead of your method parameters in the call to Replace. If you just want to remove the string, replace with the empty string:

strToRemove.Replace(strToRemove, "");

Notice that there are no quotes around the parameter name, you aren't trying to replace the string literal "strToRemove" but the value of the variable strToRemove. The second mistake is found when you read the documentation which says this:

Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

Replace does not modify the original string, so you need to assign the result and return that

string fixedString = strToRemove.Replace("str", "x");
return fixedString;
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
0

As you can see Function Exercise12 have 'string' before it, this means it returns a string as a result.

It also has 2 different strings in between parentheses (2 parameters) so this Function is supposed to read 2 strings, manipulate them, and then return a string as a result.

public static string Exercise12(string stringtoRemove, string originalString)
{
    string resultString = "";
    resultString = originalString.Replace(stringtoRemove, "");
    return resultString ;

}

As you can see, we created a new string called resultString and its value is equal to the originalString value and replacing stringtoRemove with 'nothing' at every time it exists.

Note that stringtoRemove should not have quotations so you can get its actual value.

Junior John
  • 145
  • 10