Java how to make a string alphabetic

Java how to make a string alphabetic

To make a string alphabetic in Java, you can use the replaceAll method of the String class to remove all non-alphabetic characters from the string.

Here is an example of how you can use the replaceAll method to make a string alphabetic:

refer‮l:ot ‬autturi.com
String input = "Hello, World!";
String output = input.replaceAll("[^a-zA-Z]", "");
// output is now "HelloWorld"

In this example, the replaceAll method is used to replace all characters in the input string that are not in the range a-z or A-Z with an empty string. This removes all non-alphabetic characters from the string, leaving only the alphabetic characters.

You can also use the toLowerCase method of the String class to convert the string to lowercase before removing the non-alphabetic characters, as shown in the following example:

String input = "Hello, World!";
String output = input.toLowerCase().replaceAll("[^a-z]", "");
// output is now "helloworld"

In this example, the toLowerCase method is used to convert the input string to lowercase before the non-alphabetic characters are removed using the replaceAll method. This results in a string that is all lowercase and contains only alphabetic characters.

You can also use the toUpperCase method to convert the string to uppercase before removing the non-alphabetic characters, as shown in the following example:

String input = "Hello, World!";
String output = input.toUpperCase().replaceAll("[^A-Z]", "");
// output is now "HELLOWORLD"

In this example, the toUpperCase method is used to convert the input string to uppercase before the non-alphabetic characters are removed using the replaceAll method. This results in a string that is all uppercase and contains only alphabetic characters.

Created Time:2017-11-03 23:27:10  Author:lautturi