如何用字符串替换字符串中的字符?用字符串替换字符
例如: 用“test”替换所有字符的'e':“Hello World” - >“Htestllo World”。
A,string.replace(char,string),如果你愿意的话。
如何用字符串替换字符串中的字符?用字符串替换字符
例如: 用“test”替换所有字符的'e':“Hello World” - >“Htestllo World”。
A,string.replace(char,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");
感谢您的示例,我最终不得不做更复杂的事情,因为我正在从文件中读取UTF8/UTF16内容。但这是我最初寻找的。谢谢。 –
您可以使用替换法的字符串版本:
"Hello World".Replace("e", "a long string");
您可以使用String.Replace到字符串中替换字符串中的任意次数,用另一个字符串。
返回新的字符串其中在 当前实例指定字符串的所有出现与另一指定的字符串替换。
用法示例:
string original = "Hello world";
string changed = original.Replace("e", "t");
Console.WriteLine(changed); // "Htllo world"
任何机会,你可以只处理字符作为一个字符串,并使用'与string.replace(字符串,字符串)'一个? – chris
可能的重复:http://stackoverflow.com/questions/12550042/replace-method-in-delegate-string/ – Borgleader
注意到你想用一个字符串替换一个字符串“e”。所以建立一个单字符的临时字符串。 –