简体   繁体   English

从字符串中删除单个特殊字符

[英]Remove a single Special Character from a String

I am new to C# and I want to know that how can I remove a single apostrophe ( ' ) from my string. 我是C#的新手,我想知道如何从字符串中删除单个撇号( ' )。 I have a problem that I am using my code to remove other special characters and it works fine except this special character ( ' ). 我有一个问题,我正在使用我的代码删除其他特殊字符,并且除此特殊字符( ' )之外,它都可以正常工作。

My code is: 我的代码是:

mystring=mystring.Replace(@"'"," ");

How can i remove this character from my string is there any other way can anybody please help me? 我如何从字符串中删除此字符,还有其他任何方法可以帮助我吗?

The character you are showing us in the comment is another one than the one you are using in the code 您在注释中显示给我们的字符与您在代码中使用的字符不同

(’)     => is ANSI 146  (in comment, 92 hex) 

(')     => is ANSI  39  (in code)

Solution 1: Copy paste the character from the source into the code. 解决方案1:将字符从源代码复制粘贴到代码中。

Solution 2: Use a unicode escape sequence: 解决方案2:使用unicode转义序列:

mystring = mystring.Replace("\u0092", " ");

or, using chars instead of strings: 或者,使用字符而不是字符串:

mystring = mystring.Replace('\u0092', ' ');

Note, in your example you are replacing the apostrophe by a space. 请注意,在您的示例中,您将用空格替换撇号。 If you want to remove it instead do: 如果要删除它,请执行以下操作:

mystring = mystring.Replace("\u0092", "");

See: ANSI character set and equivalent Unicode and HTML characters . 请参阅: ANSI字符集和等效的Unicode和HTML字符

That is not a regular apostrophe. 那不是常规的撇号。

You need something more like this. 您需要更多类似这样的东西。

mystring = mystring.Replace("\x92", "");

You can use the Regex.Replace method 您可以使用Regex.Replace方法

string output = Regex.Replace(mystring, @"'", "");

I hope I helped 我希望我能帮助

//we can remove . //我们可以删除。 or any special character from string using Replace in csharp// 或使用csharp //中的Replace从字符串中获取任何特殊字符

string name = " .Akhil. ";
 name = name.Replace( " .Akhil. ", "Akhil");
Console.WriteLine(name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM