根据我在这里看到的(接受的答案),我似乎可以通过这样做来转义字符串:
string s = "Woolworth's";
string t = Regex.Escape(s);
MessageBox.Show(t);
...但是通过它,我发现 s 和 t 之间没有区别(我希望我会看到“Woolworth\'s”作为 t 的值,而不是两个 vars 的“Woolworth's”)。
我想,我可以做这样的事情:
string s = "Woolworth's";
s = s.Replace("'", "\'");
...etc., also escaping the following: [, ^, $, ., |, ?, *, +, (, ), and \
...但“一站式购物”解决方案会更可取。
更具体地说,我需要用户输入的字符串可以作为 Android arrays.xml 文件中的字符串值。
例如,它对此感到窒息:
<item>Woolworth's</item>
...这需要是这样的:
<item>Woolworth\'s</item>