这是其中一个问题.基本上我卡住了.
编写一个方法,从另一个字符串中删除所有出现的字符串:
[Test]
public void TestExercise12()
{
Programmeren2Tests.Chapter12Test.TestExercise12(Exercise12);
}
public static string Exercise12(string strToRemove, string str)
{
strToRemove.Replace("str", "x");
return str;
}
Run Code Online (Sandbox Code Playgroud)
我在这里做了什么显然没有用,但我不知道在哪里搜索如何做到这一点.
字符串在C#/ .NET中是不可变的,因此foo.Replace不会更新值foo,而是返回一个新值:
String foo = "abc";
String fooWithoutB = foo.Replace( "b", "" );
Assert.AreEqual( "abc", foo );
Assert.AreEqual( "ac" , fooWithoutB );
Run Code Online (Sandbox Code Playgroud)
其他几点说明:
str变量名称的前缀).这是不鼓励的.str并没有告诉我它的用途.考虑签名RemoveAllOcurrences(String needle, String haystack)或(String ofThis, String fromThis)代替.