This example has most of what we need in strings. Go through it
carefully.
import java.util.*;
public class StringReview {
public static void main (String[] argv)
{
// Simple assignment using a constant.
String str1 = "qwerty";
System.out.println ("str1=" + str1);
// Assignment using an instance of the class String.
String str2 = new String ("qwerty");
// Note use of concatenation operator and length() method in String.
String str3 = "str2=" + str2 + " has length " + str2.length();
System.out.println (str3);
// Use the equals() method to determine string equality.
boolean str1EqualsStr2 = str1.equals (str2);
System.out.println ("str1 eq str2: " + str1EqualsStr2);
// Beware of using ==
String str4 = "qwerty";
if (str1 == str4) {
System.out.println ("This seems to work");
}
String str5 = new String ("qwert");
String str6 = str5 + "y";
if (str1 == str6) {
System.out.println ("This seems to work");
}
else {
// This is what gets printed.
System.out.println ("Oops - this didn't work");
}
if ( str1.equals(str6) ) {
System.out.println ("This definitely works");
}
// Sometimes it's useful to ignore case.
String str7 = "qWeRtY";
if ( str1.equalsIgnoreCase(str7) ) {
System.out.println ("This is useful");
}
// Some useful String functions.
// Examine a char inside the string.
char ch = str1.charAt (3);
System.out.println ("Char at position 3 in String " + str1 + " is " + ch);
// Prints 'r'
// Extract a substring (without changing the original).
String subStr = str1.substring (1, 3);
System.out.println ("Chars from 1-3 in String " + str1 + ": " + subStr);
// Prints "we"
// Locate first occurence of a char.
int indexInString = str1.indexOf ('r');
System.out.println ("Index of r in " + str1 + ": " + indexInString);
// index=3
// Shortcut operator for concatenation.
str1 += str2;
System.out.println (str1);
// Prints "quertyqwerty"
// Extract char's in an array.
char[] letters = str1.toCharArray ();
System.out.println ( "Char array: " + Arrays.toString(letters) );
// Splitting by whitespace.
String str8 = new String ("Jack and Jill went up the hill");
String[] pieces = str8.split (" ");
// Notice how to print the quote character in a string:
System.out.println ("String \"" + str8 + "\" has " + pieces.length + " pieces:");
for (int i=0; i<pieces.length; i++) {
System.out.println (pieces[i]);
}
// Alphabetical-order comparison
String str9 = "aardvark";
String str10 = "Zygote";
compare (str9, str10); // Prints "Zygote precedes aardvark"
compareLowercase (str9, str10); // Prints "aardvark precedes zygote"
}
static void compare (String s1, String s2)
{
// Use the compareTo() method in the class String.
// This returns -1 if alphabetically before, +1 if after, 0 if equal.
if ( s1.compareTo(s2) < 0 ) {
System.out.println (s1 + " precedes " + s2);
}
else if ( s1.compareTo(s2) > 0 ) {
System.out.println (s2 + " precedes " + s1);
}
else {
System.out.println (s2 + " equals " + s1);
}
}
static void compareLowercase (String s1, String s2)
{
// Change to lowercase for case-free comparison.
compare (s1.toLowerCase(), s2.toLowerCase());
}
}