Some Built In Character Methods

 

 

The Character Class.  Another class you are likely to want to gain some competence with has to do with Characters and their manipulation.  The following table contains several methods in the Character class.  Since these are likely less familiar to you we will present a small program.

 

class Character  in (java.lang package)
Method

Description

Character.isDigit(ch) Returns whether something is a numerical digit 
ch is a  character, returns a boolean
Character.isLetter(ch) Returns whether something is a letter
ch is a  character, returns a boolean
Character.isLowerCase(ch) Returns whether something is lower case
ch is a  character, returns a boolean
Character.isUpperCase(ch) Returns whether something is upper case
ch is a  character, returns a boolean
Character.isSpaceChar(ch) Returns whether or not the character is a blank space
ch is a  character, returns a boolean
Character.isWhitespace(ch) Returns whether or not the character is a blank space or control character such as \n, \t or \r
ch is a  character, returns a boolean
Character.toLowerCase(ch) Converts the character to lower case
Returns a char
Character.toUpperCase(ch) Converts the character to upper case
Returns a char

 

Now you want to run the following program called CharacterSampler.java.  This will just do some manipulations on a string that I have written into the program in order to illustrate some useful issues.  It also uses some of these methods on strings that are defined within the code.

 

import javax.swing.*;

public class CharacterSampler
{

public static void main( String args[ ])
{

String messageString;
int stringLength;
String output;
boolean whiteSpace;
boolean spaceChar;
boolean isLetter;
boolean isLower;

// the string we start with
messageString = "This is a string with\nblanks\tcontrol characters\nand all KINDS of things\"";

// echo the entry to the user
output = "The original message displays as: \n" + messageString;
output = output + "\n\nLiterally this is \"This is a string with\\nblanks\\tcontrol characters\\nand all KINDS of things\"";

// get the message length
stringLength = messageString.length();
output = output + "\nThe length of the message is " + stringLength;

// checking to see if \n is considered to be whitespace
whiteSpace = Character.isWhitespace(messageString.charAt(messageString.indexOf('\n')));

output = output + "\n\nCharacter.isWhitespace(messageString.charAt(messageString.indexOf(\'\\n\'))) = whiteSpace = " + whiteSpace;

// checking to see if \n is considered to be blankspace
spaceChar = Character.isSpaceChar(messageString.charAt(messageString.indexOf('\n')));

output = output + "\nCharacter.isSpaceChar(messageString.charAt(messageString.indexOf(\'\\n\'))) = spaceChar = " + spaceChar;

// checking to see if \n is considered to be a letter
isLetter = Character.isLetter(messageString.charAt(messageString.indexOf('\n')));

output = output + "\nCharacter.isLetter(messageString.charAt(messageString.indexOf(\'\\n\'))) = isLetter = " + isLetter;


// checking to see if \n is considered to be lower case
isLower = Character.isLowerCase(messageString.charAt(messageString.indexOf('\n')));

output = output + "\nCharacter.isLowerCase(messageString.charAt(messageString.indexOf(\'\\n\'))) = isLower = " + isLower;


JOptionPane.showMessageDialog(null,output,"Evaluating Character Properties",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

 

What the code does is
  • works from the usual style of declarations and initializations and then tests characters in a string
  • It prints out the string both as it normally would and then in literal form so you can see the control characters.
    • notice the \t fails to work. 
  • It makes use of the String method indexOf( ) to locate something in the string. 
  • Then it uses this inside the charAt( ) String method to determine the character. 
  • Then the Character methods operate on these.  So we have a triple composition of methods from two different classes.
  • Then we test the newline character "\n" to find out it is considered to be
    • it is whitespace
    • isn't a letter
    • isn't a blank
    • isn't lower case

This is demonstrated in the following display of the results of the program.