1.15 String Manipulation

Suggested Skills: 2.C, 3.C, 4.A, 4.B

LO 1.15.A — Develop code to create string objects and determine the result of creating and combining strings.

  • 1.15.A.1 — A String object represents a sequence of characters and can be created by using a string literal or by calling the String class constructor.

  • 1.15.A.2 — The String class is part of the java.lang package. Classes in the java.lang package are available by default.

  • 1.15.A.3 — A String object is immutable, meaning once a String object is created, its attributes cannot be changed. Methods called on a String object do not change the content of the String object.

  • 1.15.A.4 — Two String objects can be concatenated together or combined using the + or += operator, resulting in a new String object. A primitive value can be concatenated with a String object. This causes the implicit conversion of the primitive value to a String object.

  • 1.15.A.5 — A String object can be concatenated with any object, which implicitly calls the object's toString method (a behavior guaranteed to exist by the inheritance relationship every class has with the Object class). An object's toString method returns a string value representing the object. Subclasses of Object often override the toString method with class-specific implementation. Method overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass, but the behavior of the method is specific to the subclass.

    • EXCLUSION — Overriding the toString method of a class is outside scope.

LO 1.15.B — Develop code to call methods on string objects and determine the result of calling these methods.

  • 1.15.B.1 — A String object has index values from 0 to one less than the length of the string. Attempting to access indices outside this range will result in a StringIndexOutOfBoundsException.

  • 1.15.B.2 — The following String methods are part of the Java Quick Reference:

    • int length() — returns the number of characters in a String object.

    • String substring(int from, int to) — returns the substring beginning at index from and ending at index to - 1.

    • String substring(int from) — returns substring(from, length()).

    • int indexOf(String str) — returns the index of the first occurrence of str; returns -1 if not found.

    • boolean equals(Object other) — returns true if this corresponds to the same sequence of characters as other; returns false otherwise.

    • int compareTo(String other) — returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other. Strings are ordered based upon the alphabet.

    • EXCLUSION — Using the equals method to compare one String object with an object of a type other than String is outside scope.

  • 1.15.B.3 — A string identical to the single element substring at position index can be created by calling substring(index, index + 1).

📘 Lab: After completing this unit, students will have covered all of the necessary content for the Receipt Lab.

Last updated

Was this helpful?