Lecture 31
String Methods
We looked at several methods of the String class today:
- length() -- returns the length (i.e., number of characters) of the string
- indexOf(String s) -- finds first occurrence of the string s, starting at
index 0, and moving forward through the string.
- indexOf(String s, int fromIndex) -- finds first occurrence of the string s,
starting at index fromIndex, and moving forward through the string.
- lastIndexOf(String s) -- finds first occurrence of the string s,
starting at index length() (i.e., the end of the string), and moving backward through the string.
- lastIndexOf(String s, int fromIndex) -- finds first occurrence of the string s,
starting at index fromIndex (i.e., the end of the string), and moving backward through the string.
- charAt(int index) -- returns the character (type char) at index index
- equals(Object o) -- returns true if o is type String and
the characters of o exactly match the characters of the string whose equals method
was called. Returns false otherwise.
- equalsIgnoreCase(String s) -- Similar to equals, except that parameter must
be type String and lowercase letters match their respective uppercase versions.
- endsWith(string suffix) -- returns true if the string ends with the exact
substring suffix
- startsWith(string prefix) -- returns true if the string starts with the exact
substring prefix
- substring(int beginIndex) -- returns the substring of the string that begins at index
beginIndex and goes to the end of the string.
- substring(int beginIndex, int endIndex) -- returns the substring of the string that begins at index
beginIndex and goes up to (but does not include) endIndex.
- toLowerCase() -- converts all letters in the string to their lowercase equivalent. Non-letter
characters in the string are unchanged.
- topperCase() -- converts all letters in the string to their uppercase equivalent. Non-letter
characters in the string are unchanged.
Class example
StringsDemo demonstrates how each of these works.
String Practice
We wrote a method that took in a String of text, and a String word, and counted how many times
the word appeared in the text.
public int wordCount(String text, String word)
{
   int count = 0;
   int pos = text.indexOf(word);
   while (pos >= 0)
   {
     count++;
     pos = text.indexOf(word, pos + word.length());
   }
   return count;
}
This simple version would not catch words that were the same, but were capitalized. So, we
modified our method with the toLowerCase method call to catch all occurrences of the word.
public int wordCount(String text, String word)
{
   int count = 0;
   text = text.toLowerCase();
   word = word.toLowerCase();
   int pos = text.indexOf(word);
   while (pos >= 0)
   {
     count++;
     pos = text.indexOf(word, pos + word.length());
   }
   return count;
}
We looked at class example
BuildDemoPage, which takes a class name and applet
sizes to generate the html code necessary to put a java applet in a web page.
This program had several interesting features. It uses the \n character to
create a newline in the string. It also uses the sequence \" to insert a printable
double quote inside of our string.
Finally, we looked at the
FindLinks program, which takes the source code of a web page
and identifies all of the hyperlinks in page.