Java: The Character Class
Every primitive data type has a wrapper class that provides methods for manipulating the data type. There is a lot of really useful functions in the Character class that we are going to look at.
These methods are all static methods, and thus you do not have to create an instance of the character class. Also, you do not have to import anything.
We are going to use this string below to play around with the Character class and the available methods:
We are going to do these things:Code:String s = "Hello.There!!!3233 67yt$";
1. Count the number of upper case letters
2. Count the number of lower case letters
3. Count the number of spaces
4. Count the number of digits
The Character class provides methods to do all of these things. To check if letters are in uppercase, we use the method isUpperCase? To count the lowercase letters, we use the method isLowerCase. To count the number of spaces, we use the method isSpaceChar.
As a matter of efficiency, you can do all these operations in one iteration of the String. However, I am going to use several iterations to emphasis the different methods.
All of these methods return true or false indicating what the current character is.
A method we are going to use a lot is the s.charAt method. This returns an individual char from the string.You can think of this as a direct-access to an item in an array.
Counting Upper Case Letters
The method we are going to use is the method Character.isUpperCase. We need a counter for the upper case letters.
This variable is going to count how many upper case letters there are.Code:int nUpper = 0;
The output is:Code:for (int i=0;i<s.length();i++) { if (Character.isUpperCase(s.charAt(i))) { nUpper++; } } System.out.println("s contains " + nUpper + " upper case letters.");
All we have to do is iterate through the String, checking if each letter is upper case and if it is then we update the counter by 1.s contains 2 upper case letters.
Count Lower Case Letters
We need another counter to count the number of lower case letters.
Again, a simple iteration and if structure works well here.Code:int nLower = 0;
Output:Code:for (int i = 0; i < s.length(); i++) { if (Character.isLowerCase(s.charAt(i))) { nLower++; } } System.out.println("s contains " + nLower + " lower case letters.");
Counting Spacess contains 10 lower case letters.
Note, that there is a method isSpace but it is a deprecated method and you shold use isSpaceChar instead. This again is just a simple iteration and counting task.
Output:Code:int nSpaces = 0; for (int i=0;i<s.length();i++) { if (Character.isSpaceChar(s.charAt(i))) { nSpaces++; } } System.out.println("s contains " + nSpaces + " spaces.");
Counting Digitss contains 1 spaces.
Output:Code:int nDigit = 0; for (int i=0;i<s.length();i++) { if (Character.isDigit(s.charAt(i))) { nDigit++; } } System.out.println("s contains " + nDigit + " numbers.");
This is just the same as above. I don't need to comment on it any. If Character.isDigit returns true then we find a digit and we increment the counter.s contains 6 numbers.
These methods provide a great and fast way to manipulate characters. There are lots of available methods. You just have to take a look and see what is there. This is a vary useful class.
Last edited by chili5; 09-01-2009 at 10:15 AM.
+rep!
Nice! +rep
Awesome! +rep
Wow I changed my sig!
Great post +rep, but that smilie is a little weird![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks