Chapter 7 - Strings and String Processing |
Chapter ObjectivesYou should be able to:
|
Index: |
String name1 = ""; //
Reference to the empty string
String name2 = "Socrates"; // References to "Socrates"
String name3 = "Socrates";
String name4 = new String(); // Creates an object
String name5 = new String("Socrates");
String name6 = name4;
name is 4 characters long.
String name = "Socrates";
char c = name.charAt(3); // in this case the variable c now
has an "r" in it.
String number = new String
(String.valueOf(128)); // Creates "128"
String truth = new String (String.valueOf(true)); // Creates
"true"
String bee = new String (String.valueOf('B')); //
Creates "B"
String pi = new String(String.valueOf(Math.PI)); // Creates
"3.14159"
float f = Float.parseFloat("123.456");
double d = Double.parseDouble("444.44");
long l = Long.parseLong("22222");
public int lastIndexOf(int character);
public int lastIndexOf(int character, int startingIndex);
public int lastIndexOf(String string);
public int lastIndexOf(String string, int startingIndex);
string1.indexOf('o')
==> -1 string1.lastIndexOf('o') ==> -1
string2.indexOf('o') ==> 4 string2.lastIndexOf('o') ==>
4
string3.indexOf('o') ==> 1 string3.lastIndexOf('o') ==>
1
string4.indexOf('o') ==> 4 string4.lastIndexOf('o') ==>
7
string1.indexOf("or")
==> -1 string1.lastIndexOf("or") ==> -1
string2.indexOf("or") ==> -1 string2.lastIndexOf("or") ==> -1
string3.indexOf("or") ==> 1 string3.lastIndexOf("or") ==> 1
string4.indexOf("or") ==> 7 string4.lastIndexOf("or") ==> 7
/**
* Pre: s and keyword are any Strings
* Post: keywordSearch() returns a String containing the
* number of occurrences of keyword in s, followed
* by the starting location of each occurrence
*/
public String keywordSearch(String s, String keyword) {
String resultStr = "";
int count = 0;
int ptr = s.indexOf(keyword);
while (ptr != -1) {
++count;
resultStr
= resultStr + ptr + " ";
ptr = s.indexOf(keyword,
ptr + 1); // Find next occurrence
}
resultStr = count + ": " + resultStr;
// Insert the count
return resultStr; //
Return as a String
} // keywordSearch()
Method Signature | Example | Value |
boolean endsWith( String suffix) | "Perfection".endsWith("tion") | true |
boolean startsWith(String prefix) | "Perfection".startsWith("Per") | true |
String toUpperCase() | "Perfection".toUpperCase() | "PERFECTION" |
String toLowerCase() | "Perfection".toLowerCase() | "perfection" |
String trim() | " Perfection ".trim() | "Perfection" |
" " "!" "0" "A" "Andy" "Z" "Zero" "a" "an" "and" "andy" "candy" "zero"
public boolean equals(Object anObject); //
Overrides Object.equals()
public boolean equalsIgnoreCase(String anotherString)
public int compareTo(String anotherString)
public int compareToIgnoreCase(String anotherString)
String s1 = "hello";
String s2 = "Hello";
s1.equals(s2) // false
s1.equals("hello”); // true
result = s1.equals(s2); // result
= true;
result = s3.equals(s1); // retult = false;
iresult = s1.compareTo(s2);
// iresult = 0
iresult = s1.compareTo(s3); // iresult will be a number greater than
0.
http
troy.trincoll.edu
~jjj
index.html
public int countTokens(); // Tells how many tokens are in the string
public boolean hasMoreTokens(); // tells whether or not there are any more tokens (use this to control a while loop)
public String nextToken(); // retrieves the next token.
System.out.println("there are "
+ sTokenizer.countTokens() + " tokens in the URL");
System.out.println("here they are:");
while(sTokenizer.hasMoreTokens())
{
System.out.println(sTokenizer.nextToken());
}
produces the following output:
there are 4 tokens in the URL
here they are:
http
troy.trincoll.edu
~jjj
index.html
Key terms : delimiter, equal strings, identical strings,lexicographic
order, string literal, token
Suggested Exercises : 1, 2, 3, 4, 5, 14, 16, 21