Remove all non alphabetic characters from a String array in java

后端 未结 8 914
难免孤独
难免孤独 2020-12-14 08:11

I\'m trying to write a method that removes all non alphabetic characters from a Java String[] and then convert the String to an lower case string. I\'ve tried u

相关标签:
8条回答
  • 2020-12-14 08:50

    You can also use Arrays.setAll for this:

    Arrays.setAll(array, i -> array[i].replaceAll("[^a-zA-Z]", "").toLowerCase());
    
    0 讨论(0)
  • 2020-12-14 08:55

    A cool (but slightly cumbersome, if you don't like casting) way of doing what you want to do is go through the entire string, index by index, casting each result from String.charAt(index) to (byte), and then checking to see if that byte is either a) in the numeric range of lower-case alphabetic characters (a = 97 to z = 122), in which case cast it back to char and add it to a String, array, or what-have-you, or b) in the numeric range of upper-case alphabetic characters (A = 65 to Z = 90), in which case add 32 (A + 22 = 65 + 32 = 97 = a) and cast that to char and add it in. If it is in neither of those ranges, simply discard it.

    0 讨论(0)
提交回复
热议问题