Sie sind auf Seite 1von 6

Supplement for Machine Exercise 2

Method Description Example


length() Returns the length of the String greeting = Hello!;
String object. greeting.length()
returns 6
equals(Other_String) Returns true if the calling String greeting =
object string and the dataIn.readLine();
Other_String are equal.
Otherwise, returns false. if (greeting.equals(Hi!))
System.out.prinln(Informal
greeting.);
equalsIgnoreCase Returns true if the calling If a program contains
(Other_String) object string and the String s1 = jahan;
Other_String are equal,
considering uppercase and Then after this assignment
lowercase versions of a letter s1.equalsIgnoreCase(Jahan)
to be the same. Otherwise returns true.
returns false.
Method Description Example
toLowerCase() Returns a string with the String greeting = Hi Sir!;
same characters as the greeting.toLowerCase()
calling object string, but with returns hi sir!
all characters converted to
lowercase.
toUpperCase() Returns a string with the String greeting = Hi Sir!;
same characters as the greeting.toLowerCase()
calling object string, but with returns HI SIR!
all characters converted to
uppercase.
trim() Returns a string with the String pause = Hmm ;
same characters as the pause.trim()
calling object string, but with returns Hmm
leading and trailing
whitespace removed.
Method Description Example
charAt(Position) Returns the character in the String greeting = Hello!;
calling object string at greeting.charAt(0) returns H
Position. Positions are greeting.charAt(1) returns e
counted 0, 1, 2, etc.
substring(Start) Returns the substring of the String sample = AbcdefG;
calling object from position sample.substring(2) returns
Start through to the end of cdefG
the calling object. Positions
are counted 0, 1, 2, etc.
substring(Start,End) Returns the substring of the String sample = AbcdefG;
calling object from position sample.substring(2,5) returns
Start through, but not cde
including, position End of the
calling object. Positions are
counted 0, 1, 2, etc.
Method Description Example
indexOf(A_String) Returns the position of the String greeting = Hi Sir!;
first occurrence of the string greeting.indexOf(Sir)
A_String in the calling object returns 3
string. Positions are counted greeting.indexOf(Boom)
0, 1, 2, etc. Returns -1 if returns -1
A_String is not found.
indexOf(A_String, Returns the position of the String name = Sir, Sir, quite
Start) first occurrence of the string contrary.;
A_String in the calling object
string that occurs at or after name.indexOf(Sir,1)
position Start. Positions are returns 5. The same value is
counted 0, 1, 2, etc. Returns - returned if 1 is replaced by
1 if A_String is not found. any number up to and
including 5.
name.indexOf(Sir,0)
returns 0.
name.indexOf(Sir,8)
returns -1.
Method Description Example
lastIndexOf(A_String) Returns the position of the String name = Sir, Sir, Sir not
last occurrence of the string quite so.;
A_String in the calling object greeting.lastIndexOf(Sir)
string. Positions are counted returns 10.
0, 1, 2, etc. Returns -1 if
A_String is not found.
compareTo(A_String) Compares the calling object String entry = advent;
string with A_String to see entry.compareTo(zoo)
which comes first in the returns a negative number.
lexicographic ordering. Lexicographic ordering is the entry.compareTo(advent)
same as alphabetical ordering when both strings are returns a zero.
either all uppercase or all lowercase. If the calling entry.compareTo(above)
string is first, compareTo returns a negative value. If returns a positive number.
the two strings are equal, it returns zero. If the
argument is first, it returns a positive number.

Das könnte Ihnen auch gefallen