简单!如何在.NET中替换某些东西......?
答案 0 :(得分:8)
string result = myString.Replace("\"", "foo");
答案 1 :(得分:5)
哦,你的意思是你想要replace all occurrences of the string "
with something else?
试试这个:
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);
}
}
用法:
"\"".ChangeTo("Bob");
string test = string.Concat("\"", "Hello!", "\"");
Console.WriteLine(test);
输出:
BobHello!Bob
注意:这完全是个玩笑。
答案 2 :(得分:3)
这样的事情:
var st = "String with \" in it.";
st.Replace('\"', 'c'); // replacement char
答案 3 :(得分:3)
string newValue = "quote \"here\"".Replace("\"", "'");
或者
string newValue = @"quote ""here""".Replace(@"""", "'");
答案 4 :(得分:2)
myString.Replace("\"", "something");
答案 5 :(得分:2)
使用转义字符\
。因此"
在字符串
\"
答案 6 :(得分:2)
我不明白你的问题,但是如果转义序列是你正在寻找的......在这种情况下要转义"
然后转换C#
看{4}}和{{1与C#进行比较并查看字符串
here帖子的一部分。
答案 7 :(得分:2)
你想用某些东西替换双引号吗?
如果你有一个字符串变量:
string sMyText =“.....”;
您可以使用以下内容替换双引号:
sMyText = sMyText.Replace("\"","x");
斜杠字符\是一个转义字符,允许您使用“字符串内部。
答案 8 :(得分:1)
\“
或在vb.net中 “”
答案 9 :(得分:-1)
char[] str = myString.ToCharArray();
StringBuilder newString = new StringBuilder("");
for(int i = 0;i<str.Length;i++)
{
if('"'.Equals(str[i])) newString.Append(''); // put your new char here
else newString.Append(str[i]);
}
myString = newString.ToString();
str = null;
// Don't use this method. This is really stupid. I just felt like being a little snarky.