如何从字符串中除去除破折号之外的所有非字母数字字符?

时间:2022-09-24 23:29:23

How do I remove all non alphanumeric characters from a string except dash and space characters?

如何从字符串中除去除破折号和空格字符之外的所有非字母数字字符?

11 个解决方案

#1


671  

Replace [^a-zA-Z0-9 -] with an empty string.

取代[^ a-zA-Z0-9 -]和一个空字符串。

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");

#2


311  

I could have used RegEx, they can provide elegant solution but they can cause performane issues. Here is one solution

我可以使用RegEx,它们可以提供优雅的解决方案,但是它们会导致性能问题。这是一个解决方案

char[] arr = str.ToCharArray();

arr = Array.FindAll<char>(arr, (c => (char.IsLetterOrDigit(c) 
                                  || char.IsWhiteSpace(c) 
                                  || c == '-')));
str = new string(arr);

When using the compact framework (which doesn't have FindAll)

使用compact框架时(它没有FindAll)

Replace FindAll with1

取代FindAll看病

char[] arr = str.Where(c => (char.IsLetterOrDigit(c) || 
                             char.IsWhiteSpace(c) || 
                             c == '-')).ToArray(); 

str = new string(arr);

1Comment by ShawnFeatherly

1 ShawnFeatherly发表评论

#3


38  

You can try:

你可以尝试:

   string s1= Regex.Replace(s,"[^A-Za-z0-9 _]","");

Where s is your string.

s是弦。

#4


32  

Using System.Linq

使用来

string withOutSpecialCharacters = new string(stringWithSpecialCharacters.Where(c =>char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-').ToArray());

#5


17  

The regex is [^\w\s\-]*:

正则表达式是[^ \ w \ s \ -)*:

\s is better to use instead of space (), because there might be a tab in the text.

\s最好使用代替空格(),因为在文本中可能有一个选项卡。

#6


11  

Based on the answer for this question, I created a static class and added these. Thought it might be useful for some people.

基于这个问题的答案,我创建了一个静态类并添加了这些。我想这对有些人可能有用。

public static class RegexConvert
{
    public static string ToAlphaNumericOnly(this string input)
    {
        Regex rgx = new Regex("[^a-zA-Z0-9]");
        return rgx.Replace(input, "");
    }

    public static string ToAlphaOnly(this string input)
    {
        Regex rgx = new Regex("[^a-zA-Z]");
        return rgx.Replace(input, "");
    }

    public static string ToNumericOnly(this string input)
    {
        Regex rgx = new Regex("[^0-9]");
        return rgx.Replace(input, "");
    }
}

Then the methods can be used as:

然后,该方法可用于:

string example = "asdf1234!@#$";
string alphanumeric = example.ToAlphaNumericOnly();
string alpha = example.ToAlphaOnly();
string numeric = example.ToNumericOnly();

#7


4  

I´ve made a different solution, by eliminating the Control characters, which was my original problem.

我´已经做出了一个不同的解决方案,通过消除控制字符,这是我最初的问题。

It is better than putting in a list all the "special but good" chars

这比把所有“特别但好的”chars都列出来要好

char[] arr = str.Where(c => !char.IsControl(c)).ToArray();    
str = new string(arr);

it´s simpler, so I think it´s better !

´s简单,所以我认为它´s更好!

#8


2  

Want something quick?

想要快速的东西吗?

public static class StringExtensions 
{
    public static string ToAlphaNumeric(this string self, params char[] allowedCharacters)
    {
        return new string(Array.FindAll(self.ToCharArray(), c => char.IsLetterOrDigit(c) || allowedCharacters.Contains(c)));
    }
}

This will allow you to specify which characters you want to allow as well.

这将允许您指定您希望允许哪些字符。

#9


1  

Here is a non-regex heap allocation friendly fast solution which was what I was looking for.

这是一个非regex堆分配友好的快速解决方案,我正在寻找它。

Unsafe edition.

不安全的版本。

public static unsafe void ToAlphaNumeric(ref string input)
{
    fixed (char* p = input)
    {
        int offset = 0;
        for (int i = 0; i < input.Length; i++)
        {
            if (char.IsLetterOrDigit(p[i]))
            {
                p[offset] = input[i];
                offset++;
            }
        }
        ((int*)p)[-1] = offset; // Changes the length of the string
        p[offset] = '\0';
    }
}

And for those who don't want to use unsafe or don't trust the string length hack.

对于那些不想使用不安全或不相信字符串长度的黑客。

public static string ToAlphaNumeric(string input)
{
    int j = 0;
    char[] newCharArr = new char[input.Length];

    for (int i = 0; i < input.Length; i++)
    {
        if (char.IsLetterOrDigit(input[i]))
        {
            newCharArr[j] = input[i];
            j++;
        }
    }

    Array.Resize(ref newCharArr, j);

    return new string(newCharArr);
}

#10


0  

I use a variation of one of the answers here. I want to replace spaces with "-" so its SEO friendly and also make lower case. Also not reference system.web from my services layer.

我用其中一个答案的变体。我想用“-”替换空格,这样它的SEO友好,也可以用小写。也不参考系统。来自我的服务层的web。

private string MakeUrlString(string input)
{
    var array = input.ToCharArray();

    array = Array.FindAll<char>(array, c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-');

    var newString = new string(array).Replace(" ", "-").ToLower();
    return newString;
}

#11


-1  

There is a much easier way with Regex.

使用Regex有更简单的方法。

private string FixString(string str)
{
    return string.IsNullOrEmpty(str) ? str : Regex.Replace(str, "[\\D]", "");
}

#1


671  

Replace [^a-zA-Z0-9 -] with an empty string.

取代[^ a-zA-Z0-9 -]和一个空字符串。

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");

#2


311  

I could have used RegEx, they can provide elegant solution but they can cause performane issues. Here is one solution

我可以使用RegEx,它们可以提供优雅的解决方案,但是它们会导致性能问题。这是一个解决方案

char[] arr = str.ToCharArray();

arr = Array.FindAll<char>(arr, (c => (char.IsLetterOrDigit(c) 
                                  || char.IsWhiteSpace(c) 
                                  || c == '-')));
str = new string(arr);

When using the compact framework (which doesn't have FindAll)

使用compact框架时(它没有FindAll)

Replace FindAll with1

取代FindAll看病

char[] arr = str.Where(c => (char.IsLetterOrDigit(c) || 
                             char.IsWhiteSpace(c) || 
                             c == '-')).ToArray(); 

str = new string(arr);

1Comment by ShawnFeatherly

1 ShawnFeatherly发表评论

#3


38  

You can try:

你可以尝试:

   string s1= Regex.Replace(s,"[^A-Za-z0-9 _]","");

Where s is your string.

s是弦。

#4


32  

Using System.Linq

使用来

string withOutSpecialCharacters = new string(stringWithSpecialCharacters.Where(c =>char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-').ToArray());

#5


17  

The regex is [^\w\s\-]*:

正则表达式是[^ \ w \ s \ -)*:

\s is better to use instead of space (), because there might be a tab in the text.

\s最好使用代替空格(),因为在文本中可能有一个选项卡。

#6


11  

Based on the answer for this question, I created a static class and added these. Thought it might be useful for some people.

基于这个问题的答案,我创建了一个静态类并添加了这些。我想这对有些人可能有用。

public static class RegexConvert
{
    public static string ToAlphaNumericOnly(this string input)
    {
        Regex rgx = new Regex("[^a-zA-Z0-9]");
        return rgx.Replace(input, "");
    }

    public static string ToAlphaOnly(this string input)
    {
        Regex rgx = new Regex("[^a-zA-Z]");
        return rgx.Replace(input, "");
    }

    public static string ToNumericOnly(this string input)
    {
        Regex rgx = new Regex("[^0-9]");
        return rgx.Replace(input, "");
    }
}

Then the methods can be used as:

然后,该方法可用于:

string example = "asdf1234!@#$";
string alphanumeric = example.ToAlphaNumericOnly();
string alpha = example.ToAlphaOnly();
string numeric = example.ToNumericOnly();

#7


4  

I´ve made a different solution, by eliminating the Control characters, which was my original problem.

我´已经做出了一个不同的解决方案,通过消除控制字符,这是我最初的问题。

It is better than putting in a list all the "special but good" chars

这比把所有“特别但好的”chars都列出来要好

char[] arr = str.Where(c => !char.IsControl(c)).ToArray();    
str = new string(arr);

it´s simpler, so I think it´s better !

´s简单,所以我认为它´s更好!

#8


2  

Want something quick?

想要快速的东西吗?

public static class StringExtensions 
{
    public static string ToAlphaNumeric(this string self, params char[] allowedCharacters)
    {
        return new string(Array.FindAll(self.ToCharArray(), c => char.IsLetterOrDigit(c) || allowedCharacters.Contains(c)));
    }
}

This will allow you to specify which characters you want to allow as well.

这将允许您指定您希望允许哪些字符。

#9


1  

Here is a non-regex heap allocation friendly fast solution which was what I was looking for.

这是一个非regex堆分配友好的快速解决方案,我正在寻找它。

Unsafe edition.

不安全的版本。

public static unsafe void ToAlphaNumeric(ref string input)
{
    fixed (char* p = input)
    {
        int offset = 0;
        for (int i = 0; i < input.Length; i++)
        {
            if (char.IsLetterOrDigit(p[i]))
            {
                p[offset] = input[i];
                offset++;
            }
        }
        ((int*)p)[-1] = offset; // Changes the length of the string
        p[offset] = '\0';
    }
}

And for those who don't want to use unsafe or don't trust the string length hack.

对于那些不想使用不安全或不相信字符串长度的黑客。

public static string ToAlphaNumeric(string input)
{
    int j = 0;
    char[] newCharArr = new char[input.Length];

    for (int i = 0; i < input.Length; i++)
    {
        if (char.IsLetterOrDigit(input[i]))
        {
            newCharArr[j] = input[i];
            j++;
        }
    }

    Array.Resize(ref newCharArr, j);

    return new string(newCharArr);
}

#10


0  

I use a variation of one of the answers here. I want to replace spaces with "-" so its SEO friendly and also make lower case. Also not reference system.web from my services layer.

我用其中一个答案的变体。我想用“-”替换空格,这样它的SEO友好,也可以用小写。也不参考系统。来自我的服务层的web。

private string MakeUrlString(string input)
{
    var array = input.ToCharArray();

    array = Array.FindAll<char>(array, c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-');

    var newString = new string(array).Replace(" ", "-").ToLower();
    return newString;
}

#11


-1  

There is a much easier way with Regex.

使用Regex有更简单的方法。

private string FixString(string str)
{
    return string.IsNullOrEmpty(str) ? str : Regex.Replace(str, "[\\D]", "");
}