Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.
Goals for this problem set:
Strings to represent and manipulate text dataString methods| Method name | Description |
|---|---|
charAt(index)
|
character at given index |
indexOf(str)
|
index where the start of the given String appears in this
string (-1 if not found)
|
length()
|
number of characters in this String
|
replace(str1, str2)
|
a new string with all occurrences of str1 changed to str2 |
substring(index1, index2) or substring(index1) |
the characters in this string from index1 (inclusive) to index2 (exclusive); if index2 is omitted, grabs till end of string |
toLowerCase()
|
a new string with all lowercase letters |
toUpperCase()
|
a new string with all uppercase letters |
Write the results of each expression with Strings in
"quotes" and characters in single quotes ('a')
// index 0123456789012345
String str1 = "Frodo Baggins";
String str2 = "Gandalf the GRAY";
str1.length() |
13 |
|
str1.charAt(7) |
'a' |
|
str2.charAt(0) |
'G' |
|
str1.indexOf("o") |
2 |
|
str2.toUpperCase() |
"GANDALF THE GRAY" |
|
str1.toLowerCase().indexOf("B") |
-1 |
|
str1.substring(4) |
"o Baggins" |
|
str2.substring(3, 14) |
"dalf the GR" |
|
str2.replace("a", "oo") |
"Goondoolf the GRAY" |
|
str2.replace("gray", "white") |
"Gandalf the GRAY" |
|
"str1".replace("r", "range") |
"strange1" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class StringOops {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Type your name: ");
String name = console.nextString();
process(name);
}
public static void process(string "name") {
if (name == Whitaker) {
System.out.println("You must be really awesome.");
}
replace("a", "e");
toUppercase(name);
name.substring(0, 3);
System.out.println(name + " has " + name.length + " letters");
}
}
|
nextString should be nextstring should be Stringname should not be in quotesWhitaker should be in quotes==; must
use .equalsreplace without specifying a string
object (name)toUppercase should be
toUpperCasename. should come
before toUpperCase, not passed as a parameter to itname = to store the result
of toUpperCasename = to store the result
of substring() when
calling length
public class StringOops {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Type your name: ");
String name = console.next();
process(name);
}
public static void process(String "name") {
if (name.equals("Whitaker")) {
System.out.println("You must be really awesome.");
}
name = name.replace("a", "e");
name = name.toUpperCase();
name = name.substring(0, 3);
System.out.println(name + " has " + name.length() + " letters");
}
}
Copy/paste and save
ProcessName.java
in your editor, then go to the next slide.
import java.util.*; // for Scanner
public class ProcessName {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Type your name: ");
// your code goes here
System.out.println("Your name is: " + name);
}
}
continued on the next slide ...
Type your name: Jessica Miller
Your name is: Miller, J.