Lecture 9: String
In the section you will learn how to
- use the java document to
- know more about other classes,
- find out class constructor,
- find useful methods and the way to use them ,
- and more .....
- understand the String class.
How to look for a class(Use java docuement)
Use java doucment : Java
1.5 API Doc
- use classes list
- use index.
- find class in the package.
Example:
You want to use an object that represents a point. You guess that there
is a java class related to point. You look for the word Point in the
classes list and bingo! You find it here
.
How to use the class?
- You can get a summary of the class
- You can get short summary and detail description of the
contructor
- You can get short summary of all the public fields and
methods with detail descrption by clicking the corrensponding links
Example:
You find out all the contructors for the class Point, and how to get
the (x,y)-coordinate of a point.
You must read the text book
p134-146 or Characters
and Strings(and its links) (only read the part about Strig)
Sample program for String class
// this program reverse the string
public class StringsDemo {
public static void main(String[] args) {
String string = "I am a string";
int len = string.length();
String dest = "";
for (int i = (len - 1); i >= 0; i--) {
dest += string.charAt(i);
}
System.out.println(dest);
}
}
How to create a String
A string is often created from a string literal--a series of
characters enclosed in double quotes. e.g. "Something in the quotes".
when it encounters the string literal, the Java platform creates
a String object with value "Something in the quotes".
A common way to create a string is
String string = "I am a string";
Here's an example of creating a string from a character array:
char[] helloArray = { 'h', 'e', 'l', 'l', 'o' };
helloString = new String(helloArray);
System.out.println(helloString);
The last line of this code snippet displays: hello .
You can find other ways of constructing String in the Java document.
Add Strings
You can add string together like
String helloworld = "Hello" + "World";
You can also add String and primitive type together e.g
public class Info {
public static void main(String[] args) {
int myAge = 18;
String myName = "Charles";
String myInfo = myName + " is " + myAge + " years old.";
System.out.println(myInfo);
}
}
The output of the program is
Charles is 18 years old.
Or as in the StringDemo program
dest += string.charAt(i);
Other useful methods
Look through the methods in Java documents we can find some useful
methods.
Here are some examples:
Methods for changing strings
|
String concat(String str) String replace(char oldChar, char newChar) String toLowerCase()
|
String toUpperCase() String trim()
|
Methods for searching
|
int indexOf(int ch) int indexOf(int ch, int fromIndex) int lastIndexOf(int ch) int lastIndexOf(int ch, int fromIndex)
|
int indexOf(String str) int indexOf(String str, int fromIndex) int lastIndexOf(String str) int lastIndexOf(String str,int fromIndex)
|
Methods for testing
|
boolean startsWith(String prefix, int toffset) boolean startsWith(String prefix)
|
boolean endsWith(String suffix) boolean equalsIgnoreCase(String anotherString)
|
Methods for accessing
|
String substring(int beginIndex) String substring(int beginIndex, int endIndex)
|
char charAt(int index) public char[] toCharArray()
|
Test for equality
The correct way to test for equality is to use the equals
(String string) methods.
e.g.
public class EqualTest {
public static void main(String[] args) {
String string1 = "string1";
String string2 = "string2";
if(string1.equals(string2)) {
System.out.println("string1 is equal to string2");
} else {
System.out.println("string1 is not equal to string2");
}
}
}
The usual test expression if(string1 == string2) has a
different meaning. It tests if they refer to the same object.
.
Questions
public class Question {
/** returns the number of occurences of char ch in the string */
public static int numOfChar(String string, char ch) {
// your codes here
}
/** returns a new string resulting from removing all occurences of char ch */
public static String remove(String string, char ch) {
// your codes here
}
/** returns a new string resulting from moving the first n characters to the end */
public static String move(String string, int n) {
// your codes here
}
/** returns the number represented by the string */
public static int stringToNumber(String string) {
// we assume the string consists of only digits
// your codes here
}
}