CISC 1115
Introduction to Programming Using Java
Lecture #16
Strings


Strings

A Bit of Terminology

String Literals

Some Useful and Basic String Methods

For all of the following methods, the receiver must be an object of the String class.

length

charAt

indexOf, lastIndexOf

substring

equals

compareTo

trim

Some 'Fun' with String Manipulation

public class FunWithStrings {
   public static void main(String [] args) {
      String s = "Hello";                       
      System.out.println( '|' + s + '|');           // "Hello"

      s = "J" + s.substring(1);                 
      System.out.println( '|' + s + '|');          // "Jello"
      
      s = s.substring(0, s.length()-1) + "y";         
      System.out.println( '|' + s + '|');         // "Jelly"
      
      s += " Belly";
      System.out.println( '|' + s + '|');          // "Jelly Belly"
      
      int index = s.indexOf(" ");               
      s = "Peanut Butter and " + s.substring(0, index);   
      System.out.println( '|' + s + '|');    // "Peanut Butter and Jelly"
      
      index = s.indexOf("Jelly");                
      s = s.substring(index) + " Roll Morton";       
      System.out.println( '|' + s + '|');                // "Jelly Roll Morton"
      
      s = s.substring(s.lastIndexOf(" ")+1) + " Salt";   
      System.out.println( '|' + s + '|');                // "Morton Salt"
      
      index = s.indexOf(" ");                            
      s = s.substring(0, index) + " Iodized" + s.substring(index); 
      System.out.println( '|' + s + '|');               // "Morton Iodized Salt"
   }
}

Using String Methods

String processing is somewhat similar to array processing — which is not surprising because strings can be thought of as sequences of characters, while arrays are sequences of elements.

There are two basic ways of processing strings:

Sequential String Traversal

Random--Access String Processing

Linear, 'non-Sequential' Traversal

Strings also have a third technique of sorts, somewhat between the above two (arrays use this technique as well, but less often):

Program 16.1

Read in a line of text and print each of the 'words', where a word is defined as a sequence of characters terminated by a space.
import java.io.*;
import java.util.*;

public class Program_16_1 {
	public static void main(String [] args) throws Exception {
		Scanner scanner = new Scanner(new File("document.text"));

		while (scanner.hasNextLine()) {
			String line = scanner.nextLine().trim();

			int start = 0;
			int index = line.indexOf(' ');
			while (index >= 0) {
				String word = line.substring(start, index);
				System.out.println(word);
				start = index+1;
				index = line.indexOf(' ', index+1);
			}
			String word = line.substring(start);
			System.out.println(word);
			System.out.println();
		}
	}
}

The basic idea here is to:

Program 16.2

Same as Program 16.1, but now a word is defined as a sequence of letters.
import java.io.*;
import java.util.*;

public class Program_16_2 {
	public static void main(String [] args) throws Exception {
		Scanner scanner = new Scanner(new File("document.text"));

		while (scanner.hasNextLine()) {
			String line = scanner.nextLine().trim();

			int i = 0;
			while (i < line.length()) {
				char c = line.charAt(i);
				while (i < line.length() && !(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) {
					i++;
					if (i < line.length()) c = line.charAt(i);
				}

				String word = "";
				while (i < line.length() && (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) {
					word += line.charAt(i);
					i++;
					if (i < line.length()) c = line.charAt(i);
				}

				System.out.println(word);
			}
			System.out.println();
		}
	}
}

The basic idea here is to:

Working with the Individual Characters — the Character Class

Program 16.3

Same as Program 16.2, but use the Character class character-testing methods.
import java.io.*;
import java.util.*;

public class Program_16_3 {
	public static void main(String [] args) throws Exception {
		Scanner scanner = new Scanner(new File("document.text"));

		while (scanner.hasNextLine()) {
			String line = scanner.nextLine().trim();

			int i = 0;
			while (i < line.length()) {
				while (i < line.length() && !Character.isLetter(line.charAt(i))) 
					i++;

				String word = "";
				while (i < line.length() && Character.isLetter(line.charAt(i))) {
					word += line.charAt(i);
					i++;
				}

				System.out.println(word);
			}
			System.out.println();
		}
	}
}

The approach is the same as Program 16.2, except the characters are tested using the Character methods rather than direct conditionals.

Files used in this Lecture