c# - 替换字符串中的每个字母?

标签 c# string

这就是我到目前为止所写的内容:

string omgwut;
omgwut = textBox1.Text;
omgwut = omgwut.Replace(" ", "snd\\space.wav");
omgwut = omgwut.Replace("a", "snd\\a.wav");

现在,问题是这段代码会变成

"snd\space.wav"

进入

"snd\spsnd\a.wavce.wsnd\a.wavv"

在第四行。不是我想要的!现在我知道我不擅长 C#,所以这就是我问的原因。

解决方案会很棒!谢谢!

最佳答案

您仍然需要编写 getSoundForChar() 函数,但这应该可以满足您的要求。不过,我不确定您所要求的是否会按照您的意愿进行,即播放相关角色的声音。为此,您最好将它们放在 List 中。

StringBuilder builder = new StringBuilder();
foreach (char c in textBox1.Text)
{
    string sound = getSoundForChar( c );
    builder.Append( sound );
}
string omgwut = builder.ToString();

这是一个开始:

public string getSoundForChar( char c )
{
     string sound = null;
     if (sound == " ")
     {
         sound = "snd\\space.wav";
     }
     ... handle other special characters
     else
     {
         sound = string.Format( "snd\\{0}.wav", c );
     }
     return sound;
}

关于c# - 替换字符串中的每个字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/689286/

相关文章:

c# - XML 节点。 HasChild 将 InnerText 视为子节点

c# - 将项目添加到 Visual Studio 解决方案时出现的问题

c# - 写入PS/2端口

c - 用于从函数返回字符串文字的程序的输出

string - 当 YAML 文件包含 unicode 字符(如 "a\u0000b")时,在 golang 中读取 YAML 文件

c# - 根据 ListView 项过滤字符串集合

c# - 我希望数据库表中具有 NULL 的列将其设为空白

arrays - 比较字符串与字符串数组

php - 是否有可以清理内容的 PHP 类?

c# - 在不使用应用程序的 Dispose() 方法的情况下关闭/处置应用程序时调用 FACTORY 类中的方法