string newValue = "quote \"here\"".Replace("\"", "'");
Run Code Online (Sandbox Code Playgroud)
或者
string newValue = @"quote ""here""".Replace(@"""", "'");
Run Code Online (Sandbox Code Playgroud)
哦,你的意思是你想用其他东西替换所有出现的字符串"?
试试这个:
public static class EvilStringHelper {
private static readonly Action<string, int, char> _setChar;
private static readonly Action<string, int> _setLength;
static EvilStringHelper() {
MethodInfo setCharMethod = typeof(string).GetMethod(
"SetChar",
BindingFlags.Instance | BindingFlags.NonPublic
);
_setChar = (Action<string, int, char>)Delegate.CreateDelegate(typeof(Action<string, int, char>), setCharMethod);
MethodInfo setLengthMethod = typeof(string).GetMethod(
"SetLength",
BindingFlags.Instance | BindingFlags.NonPublic
);
_setLength = (Action<string, int>)Delegate.CreateDelegate(typeof(Action<string, int>), setLengthMethod);
}
public static void ChangeTo(this string text, string value) {
_setLength(text, value.Length);
for (int i = 0; i < value.Length; ++i)
text.SetChar(i, value[i]);
}
public static void SetChar(this string text, int index, char value) {
_setChar(text, index, value);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
"\"".ChangeTo("Bob");
string test = string.Concat("\"", "Hello!", "\"");
Console.WriteLine(test);
Run Code Online (Sandbox Code Playgroud)
输出:
BobHello!Bob
注意:这完全是个玩笑.
| 归档时间: |
|
| 查看次数: |
2768 次 |
| 最近记录: |