【发布时间】:2014-06-01 16:17:24
【问题描述】:
下面是我在 C# 中的代码,如果您查看附加的输出,会调用“Removing Spaces”方法,但输出并没有真正删除句子中的空格,我想不通为什么?对此的任何帮助将不胜感激。谢谢!
//using delegates for multicasting
using System;
//declare a delegate type
delegate void StrMod(ref string str);
class StringFunctions
{
static void ReplaceSpaces(ref string str)
{
Console.WriteLine("Replacing");
str = str.Replace(' ', '-');
}
static void RemoveSpaces(ref string a)
{
string temp = "";
Console.WriteLine("Removing spaces");
for (int i = 0; i < a.Length; i++)
if (a[i] != ' ') temp += a[i];
a = temp;
}
static void Reverse(ref string str)
{
string temp = "";
Console.WriteLine("Reversing");
for (int j = 0, i = str.Length - 1; i >= 0; i--, j++)
temp += str[i];
str = temp;
}
public static void Main()
{
//construct the delegates
StrMod strOp;
StrMod replace = ReplaceSpaces;
StrMod remove = RemoveSpaces;
StrMod reverse = Reverse;
string str = "this is a test";
//setting the multicast
strOp = replace;
strOp += reverse;
//invoke the multicast
strOp(ref str);
Console.WriteLine("Resultant string : " + str);
Console.WriteLine();
strOp -= reverse;
strOp += remove;
str = "This is a test"; //reset
strOp(ref str);
Console.WriteLine("Resultant string : " + str);
Console.WriteLine();
}
}
【问题讨论】:
-
我在该输出中看不到任何空格!