String Manipulation in Java for AP Computer Science A
students, strings are everywhere in programming 📱: usernames, messages, file names, and even answers on a quiz app. In AP Computer Science A, String manipulation means working with text using Java’s built-in String class. Because strings are objects, they connect directly to the unit on Using Objects and Methods. That means you are not just storing text—you are calling methods, comparing values, and building new strings from old ones.
By the end of this lesson, you should be able to:
- Explain what a
Stringis and why it is different from primitive data types. - Use common
Stringmethods such aslength(),substring(),indexOf(), andequals(). - Predict what happens when a string is changed by a method call.
- Recognize how string operations fit into the AP Computer Science A idea of objects and methods.
- Solve short code problems involving text, search, and formatting.
What Makes a String an Object?
In Java, a String is not a primitive type like $int$ or $double$. It is an object created from the String class. That means a string has methods, just like other objects you learn about in AP Computer Science A.
For example:
String city = "Boston";
Here, city stores a reference to a String object containing the characters B, o, s, t, o, n. This matters because you can call methods on it:
city.length()tells how many characters are in the string.city.substring(1, 4)extracts part of the string.city.equals("Boston")checks if the contents match exactly.
A key AP idea is that string methods do not change the original string. Strings are immutable, which means once a String is created, its characters cannot be changed. If a method seems to “change” a string, it actually creates a new string.
Example:
String name = "Mia";
String upper = name.toUpperCase();
After this, name still stores "Mia", while upper stores "MIA". This is an important difference from objects that can be modified in place.
Common String Methods You Need to Know
AP Computer Science A expects you to know the behavior of several core methods. These are some of the most useful ones.
length()
The method length() returns the number of characters in a string.
String word = "computer";
int n = word.length();
Here, n is $8$ because "computer" has eight characters.
A common mistake is to think the last position is the same as the length. In Java, string positions start at $0$, so the last index is always $length() - 1$.
substring()
The method substring() lets you take part of a string.
substring(start)gives the part fromstartto the end.substring(start, end)gives the part fromstartup to but not includingend.
Example:
String text = "sunshine";
String part = text.substring(3, 7);
The result is "shin" because the characters at indices $3$, $4$, $5$, and $6$ are included, but the character at index $7$ is not.
This “start inclusive, end exclusive” rule is very important for AP questions.
indexOf()
The method indexOf() searches for a character or substring and returns the first position where it appears.
String phrase = "banana";
int pos = phrase.indexOf("na");
The result is $2$, because the first "na" starts at index $2$.
If the text is not found, indexOf() returns $-1$. That number is a signal that the search failed.
equals() and equalsIgnoreCase()
To compare string contents, use equals().
String a = "Hello";
String b = "Hello";
boolean same = a.equals(b);
This is $true$ because the characters match exactly.
Do not use == to compare string contents. In Java, == checks whether two references point to the same object, not whether the text is the same. That is a very common AP mistake.
Use equalsIgnoreCase() when letter case should not matter.
"Yes".equalsIgnoreCase("yes")
This returns $true$.
toUpperCase() and toLowerCase()
These methods create new strings with all letters changed to upper case or lower case.
String title = "Java Rules";
String shout = title.toUpperCase();
shout becomes "JAVA RULES". The original string does not change.
String Concatenation and Building Text
Another major string skill is concatenation, which means joining strings together using the + operator.
String first = "Ada";
String last = "Lovelace";
String full = first + " " + last;
Now full is "Ada Lovelace".
Concatenation is useful for messages, output, and formatting. For example:
int score = 95;
String message = "Your score is " + score;
This produces "Your score is 95". Java automatically converts the number to text during concatenation.
Be careful with order when mixing numbers and strings:
System.out.println(2 + 3 + " apples");
System.out.println("apples: " + 2 + 3);
The first line prints 5 apples, but the second prints apples: 23. Why? Because once Java sees a string, it turns the rest into text and joins them left to right.
String Methods in Problem Solving
Many AP Computer Science A questions ask you to reason through code step by step. String manipulation often appears in tasks like searching, cleaning input, and extracting data.
Example 1: Finding a domain name
Suppose a program stores an email address:
String email = "[email protected]";
int atPos = email.indexOf("@");
String domain = email.substring(atPos + 1);
Here, atPos is $7$. The expression atPos + 1 starts right after the @ symbol. The result is "example.com".
This kind of logic is common in AP questions because it combines method calls, arithmetic, and string positions.
Example 2: Checking a password rule
A simple rule might say a password must be at least $8$ characters long:
String password = "Rocket42";
boolean valid = password.length() >= 8;
The expression password.length() >= 8 is $true$ here because the length is $8$.
Notice how string methods can work inside boolean expressions. This is a strong connection to using expressions and conditionals.
Example 3: Replacing a prefix idea with substring()
If a student ID is "AB1234", the letters and numbers can be separated:
String id = "AB1234";
String letters = id.substring(0, 2);
String numbers = id.substring(2);
letters becomes "AB" and numbers becomes "1234".
These kinds of slices are very common in data processing and formatting tasks.
Important AP Computer Science A Tips
students, here are several ideas that often show up on the exam:
- String positions start at $0$.
substring(start, end)includesstartbut excludesend.indexOf()returns the first match, or $-1$ if not found.- Use
equals()for content comparison, not==. - Strings are immutable, so methods return new strings rather than changing the original.
- Methods may be chained, such as
word.substring(1).toUpperCase(), if each part returns a string.
Example of chaining:
String word = "hello";
String result = word.substring(1).toUpperCase();
result becomes "ELLO".
Chaining works because substring(1) returns a String, and then toUpperCase() is called on that new string.
How String Manipulation Fits Into Using Objects and Methods
String manipulation is not a separate idea from objects and methods—it is one of the clearest examples of that unit.
When you write name.length(), you are calling a method on an object.
When you write text.substring(2, 5), you are using the object’s behavior to create new information.
When you write a.equals(b), you are comparing object contents in a controlled and accurate way.
This shows the AP Computer Science A pattern of thinking with objects:
- Create or receive an object.
- Call a method.
- Use the returned value in a larger program.
That pattern appears throughout Java, not only with strings. Strings are especially important because text is one of the most common forms of input and output in real programs.
Conclusion
String manipulation helps programmers work with names, messages, codes, and user input. In Java, strings are objects with useful methods such as length(), substring(), indexOf(), and equals(). Because strings are immutable, methods return new strings instead of changing the original one. These ideas are central to AP Computer Science A and directly support the unit on Using Objects and Methods.
If you can read string code carefully, track indices, and choose the correct method for the job, you will be ready for many exam questions. Practice with short examples, and remember that text processing is really object processing in Java 🙂
Study Notes
- A
Stringis an object, not a primitive type. - Strings are immutable, so methods return new strings rather than changing the original.
length()returns the number of characters.substring(start, end)includesstartand excludesend.indexOf()returns the first matching index, or $-1$ if the text is not found.- Use
equals()orequalsIgnoreCase()to compare string contents. - Avoid
==when comparing strings for text equality. - Concatenation with
+joins strings together. - Mixing numbers and strings can change how
+behaves. - String manipulation is a major example of using objects and methods in Java.
