Some Built In String Methods

 

 

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

 

class String  in (java.lang package)
Method

Description

stringName.charAt(n) Returns the character at position n in the string. 
n is an integer, returns a character
stringName.concat(anotherString) Concatenates anotherString to stringName
Returns a string
stringName.indexOf(char) Returns the position of the first occurrence of the character in stringName
Returns an integer
stringName.indexOf(string) Returns the position of the first occurrence of the string in stringName
Returns an integer
stringName.lastIndexOf(char) Returns the position of the last occurrence of the character in stringName
Returns an integer
stringName.lastIndexOf(string) Returns the position of the last occurrence of the string in stringName
Returns an integer
stringName.length( ) Returns the length of stringName
Returns an integer
stringName.split(string) Splits stringName around occurrences of the string
Returns a String array
stringName.substring(bIndex, eIndex) Returns the substring that starts at bIndex and ends at eIndex. 
Returns a String
stringName.toCharArray( ) Converts the String to an array of characters
Returns an array of char
stringName.toLowerCase( ) Converts the String to lower case. 
Returns a String
object.toString( ) Converts, if possible, an object to a String
Returns a String
stringName.toUpperCase( ) Converts the String to upper case. 
Returns a String
stringName.trim( ) Trims all leading and trailing whitespace. 
Returns a String

 

Now you want to run the following application called StringEMail.java.  This will require some user interactions.  The program does some simple validation about whether a user has input a valid e-mail address.

 

import javax.swing.*;

public class StringEMail
{

public static void main( String args[ ])
{

String strEMail;
int firstAt;
int lastAt;
int addressLength;
int lastPeriod;
String domainType;
String output;

// get a supposed e-mail address from the user
strEMail = JOptionPane.showInputDialog("Please enter an e-mail address for validation:");

// echo the entry to the user
output = "You entered: " + strEMail;

// get the e-mail address length
addressLength = strEMail.length();
output = output + "\nThe length of the e-mail address is " + addressLength;

// test indexOf to locate @ in string
firstAt = strEMail.indexOf('@');
// base output on whether an @ was inputted
if (firstAt == -1)
{

output = output + "\nA -1 implies you don't have an @ in your e-mail address!";
output = output + "\nThe first @ is located at " + firstAt;

}
else
{

// add one to the position since position count starts at 0
firstAt = firstAt + 1;
output = output + "\nThe first @ is located at " + firstAt;

}

// test lastIndexOf to locate last @ in string
lastAt = strEMail.lastIndexOf('@');

if (lastAt == -1)

output = output + "\nThe last @ is located at " + lastAt;

else
{

// add one to the position since position count starts at 0
lastAt = lastAt + 1;
output = output + "\nThe last @ is located at " + lastAt;

}

// test to make certain only one @ was entered
if (lastAt > firstAt)
output = output + "\nYou have more than one @ in your e-mail address!";
else
output = output + "\nYou have only one @ in your e-mail address!";

// find last period in purported e-mail address
lastPeriod = strEMail.lastIndexOf('.');
//  base output on whether a last period was entered
if (lastPeriod == -1)
{

output = output + "\n\nThe last . is located at " + lastPeriod;
output = output + "\nThe -1 implies you don't have a . in your e-mail address!";

}
else
{

// finding the letters after the last period in the address
domainType = strEMail.substring(lastPeriod,addressLength);
output = output + "\n\nYou claim your domain type is " + domainType;
// incrementing the position if a period was entered
lastPeriod = lastPeriod + 1;
output = output + "\nbased on the last . located at " + lastPeriod;

}

JOptionPane.showMessageDialog(null,output,"Validating E-Mail Address",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

 

We will explain the code in more detail after you run it.

You should compile the code and execute it with the following inputs.

Notice how Microsoft code senses @ symbols in order to determine e-mail addresses.

The following images relate to the third input.

 

 

In general, you should know that a stringName.indexOf(ch) and stringName.lastIndexOf(ch) return -1 if the character isn't present in the string.

We will skip over the initial declarations and initializations.  We will also assume you are used to obtaining inputs from users.  Now the code can be observed to do the following.

  • increment the output statement with the user's input
  • determine the length of the user's entry
    • increment this to the output
  • use the indexOf('@') to find the first occurrence of @
    • if no occurrence then let user know
      • -1 appears as location
    • if occurs then give actual position in usual counting
      • must increment method's value by one since it starts counting at zero
  • use the lastIndexOf('@') to find the last occurrence of @
    • if no occurrence then let user know
      • -1 appears as location
    • if occurs then give actual position in usual counting
      • must increment method's value by one since it starts counting at zero
  • compare positions of first and last @ to determine
    • if more than one was entered
    • give appropriate feedback
  • use the lastIndexOf('.') to find the last occurrence of .
    • if no occurrence then let user know
      • -1 appears as location
    • if occurs then give actual position in usual counting
      • use substring(lastPeriod,addressLength) to retrieve what should be the domain type
      • must increment method's value by one since it starts counting at zero
  • display output using showMessageDialog( ) method of JOptionPane class.

The next webpage will focus on methods for working with characters.