【问题标题】:C# how to remove all special characters EXCEPT underscore [duplicate]C#如何删除除下划线以外的所有特殊字符[重复]
【发布时间】:2021-08-20 18:43:00
【问题描述】:

我需要创建一个方法来删除除_ 之外的所有特殊字符。

显然下面的代码不好!

这确实显示了我需要的东西。

最好的方法是什么?

是否有一个正则表达式可以工作,但允许使用_

        public static string SanitizeImportName(string importName)
        {
            if (string.IsNullOrWhiteSpace(importName) == true)
            {
                throw new ArgumentNullException(nameof(importName));
            }

            return importName.Replace("!", string.Empty)
                             .Replace("@", string.Empty)
                             .Replace("#", string.Empty)
                             .Replace("$", string.Empty)
                             .Replace("%", string.Empty)
                             .Replace("^", string.Empty)
                             .Replace("&", string.Empty)
                             .Replace("*", string.Empty)
                             .Replace("(", string.Empty)
                             .Replace(")", string.Empty)
                             .Replace("-", string.Empty)
                             .Replace("+", string.Empty)
                             .Replace("=", string.Empty)
                             .Replace("/", string.Empty)
                             .Replace("\\", string.Empty)
                             .Replace("{", string.Empty)
                             .Replace("}", string.Empty)
                             .Replace("[", string.Empty)
                             .Replace("]", string.Empty)
                             .Replace("|", string.Empty)
                             .Replace(":", string.Empty)
                             .Replace(";", string.Empty)
                             .Replace("\"", string.Empty)
                             .Replace("'", string.Empty)
                             .Replace("<", string.Empty)
                             .Replace(">", string.Empty)
                             .Replace(",", string.Empty)
                             .Replace(".", string.Empty)
                             .Replace("?", string.Empty)
                             .Replace("~", string.Empty)
                             .Replace("`", string.Empty);
        }

【问题讨论】:

标签: c#


【解决方案1】:

您可以将特殊字符放入正则表达式模式中,然后使用 Replace 方法从文本中删除所有特殊字符。

var regex = new Regex("[!@#$%\^&*\(\)-+=\/\\\{\}\[\]\|:;\"'<>,.\?\~`;]");

var result = regex.Replace("Some!D#Text_With%Special$Character", string.Empty);

结果将是“SomeText_WithSpecialCharacter”。

【讨论】:

  • 我让它与我发布的答案一起工作。我无法让您的示例适用于所有内容,包括:[]
  • @ttaylor27272727 我将您在问题中写的所有字符都添加到我的答案中,并且有效。
  • 是的,我没有使用 RegEx 添加所有属性项。所以你的答案也有效!谢谢。
【解决方案2】:

以下示例将删除所有特殊字符,除了: (空格)和_

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Text.RegularExpressions;  
                    
public class Program
{
    public static void Main()
    {
        var regex = "[^0-9A-Za-z_ ]";
        var importName = "54 w/% some_ text 999";
        Console.WriteLine("Before result: "  + importName);
        var result = Regex.Replace(importName, regex, string.Empty);
        Console.WriteLine("After result: "  + result);
    }
        /*
        //var allNeedToBeRemoved = @"!@#$%^&*()-+={}|\\:;\"'<>,.?/";
        var regex = "[^0-9A-Za-z_ ]";
        //var regex = new Regex("[!@#$%^&*()+{|:;'<>,.?/~`\\/=-}]");
        //var regex = new Regex("[!@#$%^&*()+{|:;'<>,.?/~`\\/=-}]");
        //var regex = new Regex("[!@#$%^&*()+-]");      
        */  
}

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    相关资源
    最近更新 更多