I want to remove all non-alphabetic characters from a String.
Input:
"-Hello, 1 world$!"
Output:
"Helloworld"
But instead I'm getting: "Hello1world"
How can I fix it?
My code:
public class LabProgram {
public static String removeNonAlpha (String userString) {
String[] stringArray = userString.split("\\W+");
String result = new String();
for(int i = 0; i < stringArray.length;i++){
result = result+ stringArray[i];
}
return result;
}
public static void main(String args[]) {
Scanner scnr = new Scanner(System.in);
String str = scnr.nextLine();
String result = removeNonAlpha(str);
System.out.println(result);
}
}