文字列内の文字を文字列に置き換えるにはどうすればよいですか?
例:すべての文字の「e」を「test」に置き換えます:「HelloWorld」->「HtestlloWorld」。
必要に応じて、string.replace(char、string)。
文字列バージョンのReplaceメソッドを使用できます。
"Hello World".Replace("e", "a long string");
// 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");
String.Replaceを使用して、文字列内で出現する文字列を別の文字列に置き換えることができます。
現在のインスタンスで指定された文字列のすべての出現箇所が別の指定された文字列に置き換えられた新しい文字列を返します。
使用例:
string original = "Hello world";
string changed = original.Replace("e", "t");
Console.WriteLine(changed); // "Htllo world"