Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing all non letter characters from a string in C#

Tags:

c#

I want to remove all non letter characters from a string. When I say all letters I mean anything that isn't in the alphabet, or an apostrophe. This is the code I have.

public static string RemoveBadChars(string word)
{
    char[] chars = new char[word.Length];
    for (int i = 0; i < word.Length; i++)
    {
        char c = word[i];

        if ((int)c >= 65 && (int)c <= 90)
        {
            chars[i] = c;
        }
        else if ((int)c >= 97 && (int)c <= 122)
        {
            chars[i] = c;
        }
        else if ((int)c == 44)
        {
            chars[i] = c;
        }
    }

    word = new string(chars);

    return word;
}

It's close, but doesn't quite work. The problem is this:

[in]: "(the"
[out]: " the"

It gives me a space there instead of the "(". I want to remove the character entirely.

like image 728
jack3604 Avatar asked Dec 30 '14 02:12

jack3604


People also ask

How do I remove non alphabetic characters from a string?

The approach is to use the String. replaceAll method to replace all the non-alphanumeric characters with an empty string.

How do you remove all occurrences of a character from a string in C?

Logic to remove all occurrences of a characterRun a loop from start character of str to end. Inside the loop, check if current character of string str is equal to toRemove. If the mentioned condition is true then shift all character to one position left from current matched position to end of string.

How do I remove all characters from a string?

Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

What does %[ n s mean in C?

%[^\n] is an edit conversion code used to read strings.


3 Answers

The Char class has a method that could help out. Use Char.IsLetter() to detect valid letters (and an additional check for the apostrophe), then pass the result to the string constructor:

var input = "(the;':";

var result = new string(input.Where(c => Char.IsLetter(c) || c == '\'').ToArray());

Output:

the'

like image 123
Grant Winney Avatar answered Oct 07 '22 08:10

Grant Winney


You should use Regular Expression (Regex) instead.

public static string RemoveBadChars(string word)
{
    Regex reg = new Regex("[^a-zA-Z']");
    return reg.Replace(word, string.Empty);
}

If you don't want to replace spaces:

Regex reg = new Regex("[^a-zA-Z' ]");
like image 34
Dan Avatar answered Oct 07 '22 06:10

Dan


A regular expression would be better as this is pretty inefficient, but to answer your question, the problem with your code is that you should use a different variable other than i inside your for loop. So, something like this:

public static string RemoveBadChars(string word)
{
    char[] chars = new char[word.Length];
    int myindex=0;
    for (int i = 0; i < word.Length; i++)
    {
        char c = word[i];

        if ((int)c >= 65 && (int)c <= 90)
        {
            chars[myindex] = c;
            myindex++;
        }
        else if ((int)c >= 97 && (int)c <= 122)
        {
            chars[myindex] = c;
            myindex++;
        }
        else if ((int)c == 44)
        {
            chars[myindex] = c;
            myindex++;
        }
    }

    word = new string(chars);

    return word;
}
like image 27
Brandon Spilove Avatar answered Oct 07 '22 06:10

Brandon Spilove