Summary of STRING METHODS
Summary of STRING METHODS
Equals(object obj) Returns true if the object is a String representing exactly the same If (wrd.equals(wrd2))
sequence of characters
** Strings cannot be compared using ==
EqualsIgnoreCase(String stg) Returns true if the 2 strings are equal, without worrying about the If(wrd.equalsIgnoreCase(wrd2)
case of letters in the Strings
compareT(stg 2) Returns the integer result of capering the characters in a given String String result = “ ””;
if (wrd1.compareTo(wrd2) < 0)
to another string. If word 1 is alphabetically before word2, the result Result = “Alpha Order”;
is negative, if word 1 comes after word2 the result is positive. If Else
word1 equals word2 the result is zero Result = “Not alph order”;
charAt(int index) Returns the character at the specified position(index) Ch = Stg.charAt(0);
Characters are numbered form zero
substring(int p) Returns the section of a string starting at position p, to the end of the Str = Wrd.substring(2);
string
Characters are numbered form zero
substring(int p, int q) Returns the section of a string starting at position p, for Str = Wrd.substring(2,4);
//will return 2 characters
(q – p) characters
toLowerCase() Returns a string, with the case of characters converted to lowercase Word.toLowerCase();
toUpperCase() Returns a string, with the case of characters converted to uppercase Word.toUpperCase();
indexOf(int / char / String s) Returns the integer (starting) position of the first occurrence of s in a Int num = wrd.indexOf(“p”);
Int num = wrd.indexOf(“pat”);
string, starting from the beginning of the string
lastIndexOf(int / char / String s) Returns the integer (starting) position of the last occurrence of s in a Int num = wrd.lastIndexOf(“m”);
Int num = wrd.lastIndexOf(“am”,4);
string, from the end of the string