如何删除C#中的字符 [英] How to remove character in C#
问题描述
大家好
如何从以下字符串中删除\,字符:
Hi all
How do i Remove "\"," " character from following string :
"\"status\":\"ok\""
< b>我尝试了什么:
我使用下面的代码,但运行时结果是:
状态:ok;
但我需要:
状态:ok
代码:
What I have tried:
I use following code , but when run the Result is :
"status":"ok";
but i need :
status:ok
code:
string s = arr[i].Replace("'\\'", " ")
推荐答案
string s = arr[i].Replace("\"", "")
Sitecore的解决方案是正确的。看起来你的字符串中有转义字符。如果要排除所有不是字母数字的字符或':'你可以像这样使用Regex类
Sitecore's solution is correct. It looks like you are getting escape characters in your strings. If you want to exclude all characters that are not alphanumeric or ':' you can use the Regex class like this
string testString = "\"status\":\"ok\"";
var regex = new Regex("[^a-zA-Z0-9 :]");
string cleanedString = regex.Replace(testString, "");
Console.WriteLine(cleanedString);
Console.ReadLine();
它正在做的是匹配所有不是az,AZ,0-9或的字符,并用空字符串替换每个字符。
What it is doing is matching all characters that are not a-z,A-Z,0-9 or : and replacing each with an empty string.
您需要了解C#中的转义字符和文字。
在C#中转义:字符,字符串,字符串格式,关键字,标识符 [ ^ ]
取决于字符串的来源,用户输入,外部文件,代码中的文字,可执行文件中的字面值,最终结果不一样。
如果你有\status \:\ok \
在源代码中,您将有status:ok
在可执行文件中。
解决方案可能是您解决此问题所需要的。
You need to understand escape chars and literals in C#.
Escaping in C#: characters, strings, string formats, keywords, identifiers[^]
Depending on where a string come from, user input, external file, literal in code, literal in executable, the end result is not the same.
if you have"\"status\":\"ok\""
in source code, you will have"status":"ok"
in executable.
Solution is probably what you need to solve this problem.
这篇关于如何删除C#中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!