String Constructors String Methods

J.E.D.I.

4.3 The String and the StringBuffer Class

The String class provided by the Java SDK represents combinations of character literals. Unlike in other programming languages like C or C++, strings can be represented using an array of characters or by simply using the String class. Take note that a String object differs from an array of characters.

4.3.1 String Constructors

The String class has 11 constructors. To see how some of these constructors, consider the given example that follows. This example is taken from Dr. Encarnacions notes. class StringConstructorsDemo { public static void mainString args[] { String s1 = new String; creates an empty string char chars[] = { h, e, l, l, o}; String s2 = new Stringchars; s2 = hello; byte bytes[] = { w, o, r, l, d }; String s3 = new Stringbytes; s3 = world String s4 = new Stringchars, 1, 3; String s5 = new Strings2; String s6 = s2; System.out.printlns1; System.out.printlns2; System.out.printlns3; System.out.printlns4; System.out.printlns5; System.out.printlns6; } } Here is the expected output of the given program: hello world ell hello hello

4.3.2 String Methods

The following is a list of some String methods. String Methods public char charAtint index Returns the character located in the specified index. public int compareToString anotherString Compares this string with the specified parameter. Returns a negative value if this string comes lexicographically before the other string, 0 if both of the strings have the same value and a postive value if this string comes after the other string lexicographically. public int compareToIgnoreCaseString str Like compareTo but ignores the case used in this string and the specified string. Introduction to Programming II Page 60 J.E.D.I. String Methods public boolean equalsObject anObject Returns true if this string has the same sequence of characters as that of the Object specified, which should be a String object. Otherwise, if the specified parameter is not a String object or if it doesnt match the sequence of symbols in this string, the method will return false. public boolean equalsIgnoreCaseString anotherString Like equals but ignores the case used in this string and the specified string. public void getCharsint srcBegin, int srcEnd, char[] dst, int dstBegin Gets the characters from this string starting at the srcBegin index up to the srcEnd index and copies these characters to the dst array starting at the dstBegin index. public int length Returns the length of this string. public String replacechar oldChar, char newChar Returns the string wherein all occurrences of the oldChar in this string is replaced with newChar. public String substringint beginIndex, int endIndex Returns the substring of this string starting at the specified beginIndex index up to the endIndex index. public char[] toCharArray Returns the character array equivalent of this string. public String trim Returns a modified copy of this string wherein the leading and trailing white space are removed. public static String valueOf- Takes in a simple data type such as boolean, integer or character, or it takes in an object as a parameter and returns the String equivalent of the specified parameter. Table 8: Some methods of class String Consider how these methods were used in the following program. class StringDemo { public static void mainString args[] { String name = Jonathan; System.out.printlnname: + name; System.out.println3 rd character of name: + name.charAt2; character that first appears alphabetically has lower unicode value System.out.printlnJonathan compared to Solomon: + name.compareToSolomon; System.out.printlnSolomon compared to Jonathan: + Solomon.compareToJonathan; J has lower unicode value compared to j System.out.printlnJonathan compared to jonathan: + name.compareTojonathan; System.out.printlnJonathan compared to jonathan ignore Introduction to Programming II Page 61 J.E.D.I. case: + name.compareToIgnoreCasejonathan; System.out.printlnIs Jonathan equal to Jonathan? + name.equalsJonathan; System.out.printlnIs Jonathan equal to jonathan? + name.equalsjonathan; System.out.printlnIs Jonathan equal to jonathan ignore case? + name.equalsIgnoreCasejonathan; char charArr[] = Hi XX.toCharArray; Need to add 1 to the endSrc index of getChars Jonathan.getChars0, 2, charArr, 3; System.out.printgetChars method: ; System.out.printlncharArr; System.out.printlnLength of name: + name.length; System.out.printlnReplace as with es in name: + name.replacea, e; Need to add 1 to the endIndex parameter of substring System.out.printlnA substring of name: + name.substring0, 2; System.out.printlnTrim \ a b c d e f \: \ + a b c d e f .trim + \; System.out.printlnString representation of boolean expression 1010: + String.valueOf1010; toString method is implicitly called in the println method System.out.printlnString representation of boolean expression 1010: + 1010; Note theres no change in the String object name even after applying all these methods. System.out.printlnname: + name; } } Heres the output of the given program. name: Jonathan 3rd character of name: n Jonathan compared to Solomon: -9 Solomon compared to Jonathan: 9 Jonathan compared to jonathan: -32 Jonathan compared to jonathan ignore case: 0 Is Jonathan equal to Jonathan? true Is Jonathan equal to jonathan? false Is Jonathan equal to jonathan ignore case? true content of charArr after getChars method: Hi Jo Length of name: 8 Replace as with es in name: Jonethen A substring of name: Jo Trim a b c d e f : a b c d e f String representation of boolean expression 1010: false String representation of boolean expression 1010: false name: Jonathan Introduction to Programming II Page 62 J.E.D.I.

4.3.3 The StringBuffer Class