2

EDIT: I changed the title to reflect specifically what it is I'm trying to do.

Is there a way to retrieve all alphanumeric (or preferably, just the alphabet) characters for the current culture in .NET? My scenario is that I have several strings that I need to remove all numerals and non-alphabet characters from, and I'm not quite sure how I would implement this while honoring the alphabet of languages other than English (short of creating arrays of all alphabet characters for all supported languages of .NET, or at least the languages of our current clients lol)

UPDATE:

Specifically, what I'm trying to do is trim all non-alphabet chars from the start of the string up until the first alphabet character, and then from the last alphabet character to the end of the string. So for a random example in en-US, I want to turn:

()&*1@^#47*^#21%Littering aaaannnnd(*&^1#*32%#**)7(#9&^

into the following:

Littering aaaannnnd

This would be simple enough to do for English since it's my first language, but really in any culture I need to be able to remove numerals and other non-alphanumeric characters from the string.

4
  • 2
    Duplicate - stackoverflow.com/questions/5676692/… Jun 18, 2013 at 14:26
  • Can you show a few examples of the different strigs that you need to remove the non Alpha Characters from perhaps you can try some other alternatives..
    – MethodMan
    Jun 18, 2013 at 14:27
  • I'll update the question with an example
    – codewario
    Jun 18, 2013 at 14:38
  • I updated my question to address specifically what it is I'm trying to accomplish, so please don't mark this as a duplicate.
    – codewario
    Jun 18, 2013 at 14:52

3 Answers 3

1
   string something = "()&*1@^#47*^#21%Littering aaaannnndóú(*&^1#*32%#**)7(#9&^";
   string somethingNew = Regex.Replace(something, @"[^\p{L}-\s]+", "");

Is this what you're looking for?

Edit: Added to allow other languages characters. This will output Littering aaaannnndóú

8
  • 1
    Just noticed you said anything before and after. This example will strip out all illegal characters in between as well. So if Littering aaaannnnd was Li@34tterin 98#45 aaaann$45)nnd it would still come out Littering aaaannnd... Not sure if that will work for you. Jun 18, 2013 at 15:07
  • 1
    He said he wanted a solution that works in all cultures not just for US-English. So imagine he wanted Cyrillic characters to be ok in Russian culture, French characters in French, etc.
    – Shlomo
    Jun 18, 2013 at 15:13
  • Thanks Shlomo, I've updated my answer to accept other languages characters. Jun 18, 2013 at 15:25
  • 1
    \p{L} or \p{Letter}: any kind of letter from any language. \s: space. Putting it in [^...] means it will match anything in the ... area Jun 18, 2013 at 15:34
  • 1
    This is not correct. This will remove non-alpha and numeric characters from the middle of the string also. The OP clearly states both in the title and in the body of the question that it should only remove those characters from the start and end of the string.
    – rory.ap
    May 2, 2016 at 16:53
1

Using regex method, this should work out:

string input = "()&*1@^#47*^#21%Littering aaaannnnd(*&^1#*32%#**)7(#9&^";
string result = Regex.Replace(input, "(?:^[^a-zA-Z]*|[^a-zA-Z]*$)", ""); //TRIM FROM START & END
3
  • See @Shlomo's comment above, US-English is not the only culture I need this to work for.
    – codewario
    Jun 18, 2013 at 15:21
  • @AlexanderMiles check my answer above. I've updated it to where I hope it will work for you. Jun 18, 2013 at 15:32
  • This will also trim from the middle which is not what the OP intended.
    – rory.ap
    May 2, 2016 at 16:54
0

Without using regex: In Java, you could do:

while (true) {
    if (word.length() == 0) {
        return ""; // bad
    }

    if (!Character.isLetter(word.charAt(0))) {
        word = word.substring(1);
        continue; // so we are doing front first
    }
    if (!Character.isLetter(word.charAt(word.length()-1))) {
        word = word.substring(0, word.length()-1);
        continue; // then we are doing end
    }
    break; // if front is done, and end is done
}

If you are using something else, then java, substituting Character.isLetter is very straight forward, just search for character encoding and you will find the integer values for alphabetic characters, and you can use that to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.