0

文字列内の文字を文字列に置き換えるにはどうすればよいですか?

例:すべての文字の「e」を「test」に置き換えます:「HelloWorld」->「HtestlloWorld」。

必要に応じて、string.replace(char、string)。

4

3 に答える 3

6

文字列バージョンのReplaceメソッドを使用できます。

"Hello World".Replace("e", "a long string");
于 2012-09-23T20:15:03.407 に答える
2
// let's pretend this was a char that came to us from somewhere, already as a char...
char c = char.Parse("e");

// Here is the string we want to change...
string str1 = "Hello World."

// now we'll have to convert the char we have, to a string to perform the replace...
string charStr = c.ToString();

// now we can do the replace...
string str2 = str1.Replace(charStr,"test");
于 2012-09-23T20:35:24.443 に答える
1

String.Replaceを使用して、文字列内で出現する文字列を別の文字列に置き換えることができます。

現在のインスタンスで指定された文字列のすべての出現箇所が別の指定された文字列に置き換えられた新しい文字列を返します。

使用例:

string original = "Hello world";
string changed = original.Replace("e", "t");
Console.WriteLine(changed); // "Htllo world"
于 2012-09-23T20:18:30.777 に答える