1

prob a silly question but here goes:

I am looking to ignore any non-alphabetic characters and also capital letters from a string.

        if (names[j].matches("[a-zA-Z]+")){
            System.out.println(names[j] + ":");
        }

I had something like that to test if there was a non-alphabetic character given and it works, just not sure on how to ignore them as such.

Any suggestions would be brilliant. Thank you.

Taky
  • 5,284
  • 1
  • 20
  • 29
Natalie Carr
  • 3,707
  • 3
  • 34
  • 68
  • What do you mean ignore? take them out of the string or not do the condition if they are present? – gtgaxiola Jan 15 '14 at 14:23
  • I mean ignore in such that if NAtalie7@ was entered then natalie would be considered? I thought taking them out and changing the string to all lower case would be the best method but was wondering if it is possible to ignore them without removal. The code above was a quick test to check if the input wasnt all letters. – Natalie Carr Jan 15 '14 at 14:28

2 Answers2

1

If you want to ignore them and strip them out, just use a regex to match them and replace them with an empty string. String#replaceAll can take a regex.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • I think they don't want upper case either, so would it be `names[j].replaceAll("[a-z]+","");` instead? – ansible Jan 15 '14 at 14:27
  • @ansible I read the opposite I was replacing the alphabet characters instead of the other way around – gtgaxiola Jan 15 '14 at 14:30
  • Sorry, I want to ignore anything non-alphabetic but if it is uppercase i want to keep it but it is not significant when matching with other words e.g Natalie will equal natalie – Natalie Carr Jan 15 '14 at 14:32
1

Exapnding on @Jeff Storey's answer:

You do a replace on the regex [^a-zA-Z]+ with the Empty String If you really don't want to ignore the upper cases

After the replacement you convert the String into lower case

public static void main(String[] args) throws Exception {
    String hello = "NAtalie7@";
    hello = hello.replaceAll("[^a-zA-Z]+","").toLowerCase();
    System.out.println(hello);
}

Will output:

natalie

If you want to ignore the first character regardless of what it is then you can use:

String hello = "NAtalie7@";
String ignoreFirstChar = hello.charAt(0) + (hello.substring(1,hello.length()).replaceAll("[^a-zA-Z]+", "").toLowerCase());
System.out.println(ignoreFirstChar);

Will output:

Natalie
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • Thank you exactly what I was after, I had thought that was the best option but wanted to double check. Apologies for the silly question :) – Natalie Carr Jan 15 '14 at 14:36
  • Sorry quick question, is there a way to skip the first character? So only do the replace after the first character regardless whether it needs replaced or not? – Natalie Carr Jan 15 '14 at 15:45
  • Sure.. let me test it and I get you the answer – gtgaxiola Jan 15 '14 at 15:47
  • Thank you, have played about with it and tried to break it up into characters and then started from index 1, however then I cannot use the replaceAll function. – Natalie Carr Jan 15 '14 at 15:48
  • 1
    hello = hello.charAt(0) + (hello.substring(1,hello.length()).replaceAll("[^a-zA-Z]+", "").toLowerCase()); – gtgaxiola Jan 15 '14 at 15:49
  • 1
    You have been a great help. These regular expressions are a brilliant tool just difficult to get the hang of :) – Natalie Carr Jan 15 '14 at 15:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45295/discussion-between-natalie-carr-and-gtgaxiola) – Natalie Carr Jan 15 '14 at 16:10