How to remove all non alphanumeric characters from a string except dash in C#?

How to remove all non alphanumeric characters from a string except dash in C#?

To remove all non-alphanumeric characters from a string in C# except the dash character, you can use a regular expression and the Regex.Replace method. Here's an example:

 using System.Text.RegularExpressions; string input = "abc-123#def$456"; // Remove all non-alphanumeric characters except dash string output = Regex.Replace(input, "[^a-zA-Z0-9-]", ""); Console.WriteLine(output); // Output: "abc-123def456" 

In this example, we have a string input that contains a mix of alphanumeric and non-alphanumeric characters, including the dash character (-).

We use the Regex.Replace method to replace all non-alphanumeric characters except the dash with an empty string. The regular expression [^a-zA-Z0-9-] matches any character that is not an uppercase or lowercase letter or a digit or a dash.

The result is a string output that contains only the alphanumeric characters and the dash character.

Examples

  1. "C# remove non-alphanumeric characters from string except dash"

    Description: Remove all non-alphanumeric characters from a string in C# except dashes using a regular expression.

    string cleanedString = Regex.Replace(inputString, "[^a-zA-Z0-9-]", string.Empty); 
  2. "Remove special characters from string in C# except dash"

    Description: Eliminate all special characters from a string in C# while preserving dashes using a regular expression.

    resultString = Regex.Replace(inputString, "[^a-zA-Z0-9-]", string.Empty); 
  3. "C# remove non-alphanumeric characters except hyphen"

    Description: Remove all non-alphanumeric characters from a string in C# except hyphens using a regular expression.

    cleanText = Regex.Replace(inputText, "[^a-zA-Z0-9-]", string.Empty); 
  4. "C# strip non-alphanumeric characters from string but keep dash"

    Description: Strip non-alphanumeric characters from a string in C# while preserving dashes using a regular expression.

    sanitizedString = Regex.Replace(inputString, "[^a-zA-Z0-9-]", string.Empty); 
  5. "C# remove special symbols from string except dash"

    Description: Remove all special symbols from a string in C# while retaining dashes using a regular expression.

    cleanedText = Regex.Replace(inputText, "[^a-zA-Z0-9-]", string.Empty); 
  6. "Strip non-alphanumeric chars from string in C# but keep dash"

    Description: Strip non-alphanumeric characters from a string in C# while keeping dashes using a regular expression.

    cleanedString = Regex.Replace(inputString, "[^a-zA-Z0-9-]", string.Empty); 
  7. "C# remove non-alphanumerics from string excluding dash"

    Description: Remove all non-alphanumeric characters from a string in C# except dashes using a regular expression.

    cleanInput = Regex.Replace(inputString, "[^a-zA-Z0-9-]", string.Empty); 
  8. "C# strip special chars from string but keep hyphen"

    Description: Strip special characters from a string in C# while preserving hyphens using a regular expression.

    sanitizedText = Regex.Replace(inputText, "[^a-zA-Z0-9-]", string.Empty); 
  9. "C# remove non-alphanumerics except dash using regex"

    Description: Remove all non-alphanumeric characters from a string in C# except dashes using a regular expression.

    cleanedInput = Regex.Replace(inputString, "[^a-zA-Z0-9-]", string.Empty); 
  10. "C# clean string from special chars except dash"

    Description: Clean a string in C# by removing all special characters except dashes using a regular expression.

    sanitizedString = Regex.Replace(inputString, "[^a-zA-Z0-9-]", string.Empty); 

More Tags

uiwebviewdelegate express properties-file azureportal inspector scenebuilder comparison-operators download-manager bearer-token mocking

More C# Questions

More Chemical reactions Calculators

More Fitness Calculators

More Livestock Calculators

More Stoichiometry Calculators