1. create a String?
String firstName = new String("Mary");
String lastName = "Jones";
2. concatenate Strings?
String fullName = firstName + " " + lastName;
3. determine the number of characters in
a String?
int nameLength = fullName.length();
4. add newlines, tabs, and double quotes to
Strings?
Newline: \n
Tab: \t
Double Quote: \"
5. convert a String to a numeric value?
int number = Integer.parseInt("234");
double value = Double.parseDouble("45.89");
6. convert a number to a String?
int sum = 3 + 5 + 4;
System.out.println(Integer.toString(sum));
//toString is automatically called below
System.out.println(sum);
int dayOfWeek = 5;
txtDayOfWeek.setText("" + dayOfWeek);
7. extract a substring from a String?
String date = "October 10, 2001";
//Extract the month
String month = date.substring(0,7);
//Extract the day
String day = date.substring(8,10);
//Three different ways to extract the year:
String year = date.substring(12,16);
year = date.substring(12, date.length());
year = date.substring(12);
8. compare Strings?
Don't use == to compare Strings
Use -- equals method instead!
== means that the two String's have the
same address in memory:
String food = "apple";
String fruit = food;
System.out.println(food == fruit);
String fruits = ("apple banana orange");
String snack = fruits.substring(0,5);
System.out.println(food == snack);
System.out.println(food.equals(snack));
ADVANCED:
Some Java compilers only create one copy
of the same String literal in memory so
String food = "apple";
String fruit = "apple";
String snack = "apple";
System.out.println
(food == fruit && fruit == snack));
9. compare Strings ignoring case?
We can also compare two strings and
IGNORE THE CASE of the letters
by using equalsIgnoreCase method:
String name = "bob";
String boy = "Bob";
System.out.println(name.equals(boy));
System.out.println
(name.equalsIgnoreCase(boy));
10. Use the compareTo method to put Strings
in order?
String x = "angel";
String y = "zipper";
x.compareTo(y)
returns a negative number ( < 0 )
if x comes BEFORE y lexicographically
(alphabetically for Strings containing
only letters)
returns a positive number ( > 0 )
if x comes AFTER y lexicographically
(alphabetically for Strings containing
only letters)
returns 0 if x is the same as y
"angel".compareTo("zipper")
"angel".compareTo("acent")
"angel".compareTo("angel")
"let".compareTo("letter")
Ordering depends on the ASCII value of the
letter.
For example, an uppercase 'B' has a lower
ASCII value than a lowercase 'a' so
Ball comes before apple
"Ball".compareTo("apple")
In the same way the digits 0 - 9 have
lower ASCII values than letters, so
Strings that start with numbers come before
Strings that start with letters.
123 comes before apple
"123".compareTo("apple")
11. Extract individual characters from a
String?
We can get the character at a particular
position in a String using the
charAt method
String girl = "Jennifer";
ch = girl.charAt(0);
ch = girl.charAt(5);
ch = girl.charAt(6);
for (int i = 0; i < girl.length(); i++)
System.out.println
("ch = " + girl.charAt(i));
12. Extract "tokens" from a String by using
a String Tokenizer?
A token is a sequence of characters
delimited by white-space.
White-space includes the characters,
new-line ('\n'), tab('\t'), and space(' ')
The String words contains 7 tokens:
String words =
"apple ball cat dog egg fish goat";
To use the StringTokenizer class,
we must first
import java.util.*;
at the top of the file
Then we create an object of the
StringTokenizer class and pass it
the String that we want to have "tokenized"
(split into tokens)
StringTokenizer tokenizer =
new StringTokenizer(words);
We can determine the number of tokens
in the String:
int numberOfTokens =
tokenizer.countTokens();
We can use the nextToken() method to get
the next token
for (int i = 0; i < numberOfTokens; i++) {
String token = tokenizer.nextToken();
System.out.println(token);
}
We can also use the hasMoreTokens() method
to extract the tokens
String misc = "7744 abc 564";
tokenizer = new StringTokenizer(misc);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
System.out.println(token);
}