String Methods
Overview: 2.7.0
- String holds characters in a sequence
- Each character has an
index
(position) starting from 0 - Length of a string is the number of characters
- This includes spaces and special characters (like
\n
)
- This includes spaces and special characters (like
- Each character has an
- Strings are
immutable
- They can’t change after being created
- All methods which modify a string (like
String.substring
) return a new String instead of modifying the existing one
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
---|---|---|---|---|---|---|---|---|---|---|---|
H | e | l | l | o | w | o | r | l | d | ! |
- Need to know how to use various string methods for the CS A exam
- These descriptions are on the exam reference sheet
- You don’t need to memorize them
- These descriptions are on the exam reference sheet
String Methods
int length()
- method returns the number of characters in the string, including spaces and special characters like punctuation.
String substring(int from, int to)
- method returns a new string with the characters in the current string starting with the character at the from index and ending at the character before the to index (if the to index is specified, and if not specified it will contain the rest of the string).
int indexOf(String str)
- method searches for the string str in the current string and returns the index of the beginning of str in the current string or -1 if it isn’t found.
int compareTo(String other)
- returns a negative value if the current string is less than the other string alphabetically, 0 if they have the same characters in the same order, and a positive value if the current string is greater than the other string alphabetically.
boolean equals(String other)
- returns true when the characters in the current string are the same as the ones in the other string. This method is inherited from the Object class, but is overridden which means that the String class has its own version of that method.
There are more methods in the String class, but these aren’t included on the exam. Here is the full documentation.
String methods - length, substring, indexOf: 2.7.1
String.length()
returns the number of characters in the string- not the last index
- That is
length-1
- That is
- not the last index
String.substring(from, to)
returns a substringfrom
a starting pointto
another (not including theto
index)String.indexOf(substring)
searches forsubstring
in a string, and returns the first index of where it finds it- Returns
-1
if the substring isn’t found.
- Returns
compareTo and equals: 2.7.2
- can compare primitives with
==
,<
, and>
,- These don’t work with reference types (like String)
- Instead you must use
Object.equals
orString.compareTo
- Instead you must use
- These don’t work with reference types (like String)
compareTo
- compares two strings character by character
- if the original string was alphabetically ordered before the second (argument string), it returns a negative number
- if it was ordered after the second, it returns a positive number
- The number is the distance the first letter is from the second
- “A” is 7 letters from “H”
- if the original string was alphabetically ordered before the second (argument string), it returns a negative number
String.compareToIgnoreCase
- not on ap exam
- Same as
String.compareTo
; not case sensitive
Equals
- compares strings character by character
- returns true or false
- equalsIgnoreCase
- not on ap exam
- Same as
String.equals
; not case sensitive
Application Programming Interface
- API is a library of prewritten classes to simplify some programming tasks
- Grouped into a package
- Can
import
these packages/classes to use them in our programs - Documentation is the main way of understanding how to use these APIs
- Otherwise we would have to read all the code ourselves to understand how it worked
Common Mistakes with Strings: 2.7.3
- Thinking a substring will include the character at the
to
index - Thinking that a method like
String.toLowerCase
will make that instance of a string lowercase- Strings are immutable!
- calling a method on a string reference which is null
- This will result in a null pointer exception!
- using
==
to test the equality of strings- This just sees if they refer to the same object
String.equals
checks to see if the values are equal
- Treating upper and lower case characters the same
String.equals
is case sensitive
Summary: 2.7.5
index
is a number that represents the position of a character in a string- First index is always 0
length
is the number of characters in a stringsubstring
is a new string which contains a part of an original string- A string only has indexes from 0 to length-1
- Accessing anything outside of that will cause an
IndexOutOfBoundsException
- Accessing anything outside of that will cause an
- Strings are
immutable
- They cannot be changed after initialization
String.toLowercase
returns a new string, it doesn’t modify the existing one
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.