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
Stringobject represents a sequence of characters and can be created by using a string literal or by calling theStringclass constructor.1.15.A.2 — The
Stringclass is part of thejava.langpackage. Classes in thejava.langpackage are available by default.1.15.A.3 — A
Stringobject is immutable, meaning once aStringobject is created, its attributes cannot be changed. Methods called on aStringobject do not change the content of theStringobject.1.15.A.4 — Two
Stringobjects can be concatenated together or combined using the+or+=operator, resulting in a newStringobject. A primitive value can be concatenated with aStringobject. This causes the implicit conversion of the primitive value to aStringobject.1.15.A.5 — A
Stringobject can be concatenated with any object, which implicitly calls the object'stoStringmethod (a behavior guaranteed to exist by the inheritance relationship every class has with theObjectclass). An object'stoStringmethod returns a string value representing the object. Subclasses ofObjectoften override thetoStringmethod 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
toStringmethod 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
Stringobject has index values from0to one less than the length of the string. Attempting to access indices outside this range will result in aStringIndexOutOfBoundsException.1.15.B.2 — The following
Stringmethods are part of the Java Quick Reference:int length()— returns the number of characters in aStringobject.String substring(int from, int to)— returns the substring beginning at indexfromand ending at indexto - 1.String substring(int from)— returnssubstring(from, length()).int indexOf(String str)— returns the index of the first occurrence ofstr; returns-1if not found.boolean equals(Object other)— returnstrueifthiscorresponds to the same sequence of characters asother; returnsfalseotherwise.int compareTo(String other)— returns a value< 0ifthisis less thanother; returns zero ifthisis equal toother; returns a value> 0ifthisis greater thanother. Strings are ordered based upon the alphabet.❌ EXCLUSION — Using the
equalsmethod to compare oneStringobject with an object of a type other thanStringis outside scope.
1.15.B.3 — A string identical to the single element substring at position
indexcan be created by callingsubstring(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?