Free for students · Ad-free · WCAG 2.1 AA Compliant · Accessibility

String Manipulation

Lesson ~10 min read 8 MCQs

In simple terms: In simple terms, string manipulation is about how to create, combine, and work with pieces of text (called Strings) in your Java code using built-in tools.

Why this matters

Imagine you're building a simple welcome screen for a new app. You want it to greet users by name, maybe something like, "Welcome back, Priya!" Or think about a game that needs to display a score: "Your score: 4500". How does the program take the static text "Welcome back, " and combine it with a variable username? How does it take "Your score: " and attach a number that changes?

This is where String manipulation comes in. It's not just about storing text; it's about actively working with it—chopping it up, sticking it together, and searching through it. These are fundamental skills you'll use in almost every program you write. We'll explore how Java handles text and the powerful, built-in tools it gives you to manage it.

Concept overview

flowchart TD
    A[Start: String fullName = "Maya Angelou"] --> B{Get first initial}
    B --> C["firstInitial = fullName.substring(0, 1) --> 'M'"]
    C --> D{Find the space}
    D --> E["spaceIndex = fullName.indexOf(' ') --> 4"]
    E --> F{Get last name}
    F --> G["lastName = fullName.substring(spaceIndex + 1) --> 'Angelou'"]
    G --> H{Combine and convert to lowercase}
    H --> I["username = (firstInitial + lastName).toLowerCase()"]
    I --> J[End: "mangelou"]
This flowchart shows the steps to create a username from a full name string, "Maya Angelou". It starts by extracting the first initial 'M', finding the space, extracting the last name 'Angelou', and finally combining and converting them to the lowercase username "mangelou".

Core explanation

Hello! I'm Saavi, and I'm excited to walk you through one of the most useful tools in your Java toolkit: the String class. You'll use strings constantly, so let's build a strong foundation.

What is a String?

At its heart, a String is an object that represents a sequence of characters. You've already been using them!

String greeting = "Hello, world!";
String school = "Boston High School";

These are called string literals. They are the most common way to create a String. You can also use the String class constructor, but it's less common:

String name = new String("Carlos");

Good news: The String class is part of a package called java.lang, which is automatically imported into every Java program. You never have to write an import statement to use String.

The Most Important Rule: Strings are Immutable

This is a concept that trips up many students, so let's focus on it. String objects in Java are immutable. This means that once a String object is created, it can never be changed.

Think of a String like a message written in permanent marker on a small whiteboard. If you have a whiteboard that says "Seattle," you can't just erase the "S" and make it "eattle."

If you want to make a change, you have to grab a brand new whiteboard and write the new word on it. The original "Seattle" whiteboard still exists, unchanged.

Look at this code:

String city = "Seattle";
city.toLowerCase(); // This method returns a new string "seattle"
System.out.println(city); // What does this print?

Many people expect this to print "seattle". But it will print "Seattle"! The toLowerCase() method didn't change the original city string. It created and returned a new string, "seattle," which we didn't save.

Strings are Immutable: Methods return new Strings, they don't modify the original.

To "change" our variable, we have to reassign it to the new string that was created:

String city = "Seattle";
city = city.toLowerCase(); // city now points to the NEW string "seattle"
System.out.println(city); // This prints "seattle"

Combining Strings: Concatenation

"Concatenation" is a fancy word for joining strings together. Java makes this easy with the + operator.

String firstName = "Aaliyah";
String lastName = "Smith";
String fullName = firstName + " " + lastName; // "Aaliyah Smith"

int score = 95;
String report = "Your score is: " + score; // "Your score is: 95"



<figure class="lesson-figure"><div class="shr-widget" data-shr-widget="{&quot;type&quot;:&quot;array_animation&quot;,&quot;label&quot;:&quot;Concatenating &#039;Your score is: &#039; + 95&quot;,&quot;steps&quot;:[{&quot;label&quot;:&quot;Initial strings&quot;,&quot;values&quot;:[&quot;Your score is: &quot;,&quot;95&quot;],&quot;highlight&quot;:[0,1]},{&quot;label&quot;:&quot;Result of concatenation&quot;,&quot;values&quot;:[&quot;Your score is: 95&quot;],&quot;highlight&quot;:[0]}],&quot;values&quot;:[&quot;Your score is: &quot;,&quot;95&quot;],&quot;highlight&quot;:[0,1]}" aria-label="An animation showing &#039;Your score is: &#039; being combined with the integer 95 to form the string &#039;Your score is: 95&#039;."></div><figcaption class="lesson-figure-caption">String Concatenation: Joining text and numbers.</figcaption></figure>


You can also use the += operator, which is just a shortcut:

String message = "Happy ";
message += "Thanksgiving!"; // message is now "Happy Thanksgiving!"

Using String Methods to Get Information

Since we can't change strings, we need tools to read them and create new ones based on them. These tools are the String methods. To understand them, you first need to understand indexing.

Characters in a string are numbered starting from 0. For the string String s = "Hello";

  • H is at index 0
  • e is at index 1
  • l is at index 2
  • l is at index 3
  • o is at index 4

The last index is always length() - 1. Trying to access an index outside this range (like -1 or 5 in this case) will crash your program with a StringIndexOutOfBoundsException.

Here are the key methods you must know for the AP exam:

int length()

Returns the number of characters in the string.

String team = "Bulls";
int len = team.length(); // len is 5

String substring(int from, int to)

This is one of the most powerful and most confusing methods. It extracts a new string from the original.

  • from: The starting index (inclusive).
  • to: The ending index (exclusive).
String message = "AP Computer Science";
// Indices:      0123456789...
String comp = message.substring(3, 11); // "Computer"

It starts at index 3 (C) and ends just before index 11 (` `).

String substring(int from)

A simpler version that extracts from the from index all the way to the end of the string.

String message = "AP Computer Science";
String sci = message.substring(12); // "Science"

By the way, if you need to get a single character as a String, you can use substring(index, index + 1).

String name = "Jordan";
String firstLetter = name.substring(0, 1); // "J"

int indexOf(String str)

Searches for the first occurrence of str inside your string and returns the starting index. If it's not found, it returns -1.

String sentence = "The quick brown fox.";
int foxIndex = sentence.indexOf("fox"); // 16
int dogIndex = sentence.indexOf("dog"); // -1

boolean equals(Object other)

This is the only correct way to check if two strings have the exact same sequence of characters.

String pass1 = "password123";
String pass2 = "Password123";
String pass3 = "password123";

pass1.equals(pass2); // false (it's case-sensitive)
pass1.equals(pass3); // true

Never use == to compare the contents of strings. We'll cover why in the Common Mistakes section.

int compareTo(String other)

Compares two strings lexicographically (like in a dictionary).

  • Returns < 0 if this string comes before other.
  • Returns 0 if the strings are identical.
  • Returns > 0 if this string comes after other.
String a = "apple";
String b = "banana";
String c = "apple";

a.compareTo(b); // returns a negative number
b.compareTo(a); // returns a positive number
a.compareTo(c); // returns 0

This is useful for sorting lists of strings.

See it in action

python
Line 1
Output
Click Run to see the output.

        
Try these
    © Shrutam.ai

    Worked examples

    Let's apply these concepts to a couple of realistic problems.

    Example 1

    Creating a Student ID

    Problem: A school in Dallas needs to generate student IDs. The format is the first letter of the first name, the full last name, and the last two digits of their graduation year, all in lowercase. Given a student's first name, last name, and graduation year, create their ID.

    Student Data:

    • First Name: Marcus
    • Last Name: Jones
    • Graduation Year: 2025

    Solution Walkthrough:

    1. 1
      Store the initial data
      Let's start by putting our data into variables.
      String firstName = "Marcus";
      String lastName = "Jones";
      int gradYear = 2025;
    2. 2
      Get the first initial
      We need the first character of firstName. We can get this using substring(0, 1).
      String firstInitial = firstName.substring(0, 1); // "M"
    3. 3
      Get the last two digits of the year
      This is a number, but we need it as a String. A clever way is to first convert the number to a string, then take a substring of that.
      String yearString = "" + gradYear; // A shortcut to convert int to String: "2025"
      String yearDigits = yearString.substring(2); // "25"

      Why this works: Concatenating an empty string "" with the integer 2025 forces Java to convert the integer to the string "2025". Then, substring(2) gets everything from index 2 to the end.

    4. 4
      Combine the pieces
      Now we just concatenate the parts in the right order: initial + last name + year digits.
      a   String studentId = firstInitial + lastName + yearDigits; // "MJones25"
    5. 5
      Convert to lowercase
      The requirement is for the ID to be all lowercase. We can call the toLowerCase() method on our final result. Remember, this creates a new string!
      studentId = studentId.toLowerCase(); // "mjones25"

      Final Code:

      String firstName = "Marcus";
      String lastName = "Jones";
      int gradYear = 2025;
      
      String firstInitial = firstName.substring(0, 1);
      String yearString = "" + gradYear;
      String yearDigits = yearString.substring(2);
      
      String studentId = firstInitial + lastName + yearDigits;
      studentId = studentId.toLowerCase();
      
      System.out.println(studentId); // Prints "mjones25"
    Example 2

    Validating an Email

    Problem: Write code that checks if a given String email contains an "@" symbol and if it ends with ".com".

    Solution Walkthrough:

    1. 1
      Store the email
      String email = "[email protected]";
    2. 2
      Check for the "@" symbol
      The indexOf() method is perfect for this. If it finds "@", it will return its index (a number 0 or greater). If not, it returns -1.
      boolean hasAtSign = email.indexOf("@") >= 0; // true if found, false if not

      Why >= 0? We don't care where the "@" is, just that it exists. indexOf returns -1 only when the substring isn't found. So any non-negative result means it's there.

    3. 3
      Check for the ".com" ending
      This is a bit trickier. We can't just use indexOf(".com") because that would be true for "test.com.net". We need to check the end of the string. A great way to do this is to extract the last 4 characters and see if they equal ".com".
      // First, make sure the string is long enough to have a ".com"
      boolean endsWithCom = false;
      if (email.length() >= 4) {
          String ending = email.substring(email.length() - 4);
          endsWithCom = ending.equals(".com");
      }

      Why email.length() - 4? If the email is [email protected] (length 18), we want to start our substring at index 14 to get the last 4 characters. 18 - 4 = 14. This is a reliable pattern for getting the end of a string.

    4. 4
      Combine the checks
      The email is valid for our purposes only if both conditions are true.
      boolean isValid = hasAtSign && endsWithCom;
      System.out.println("Is valid email? " + isValid); // Prints "Is valid email? true"
    Tracing the creation of a student ID.

    Try it yourself

    Ready to practice? Here are a couple of challenges. Try to solve them on your own before looking up a solution.

    1. Format an Address

    You are given three strings: city ("Chicago"), state ("IL"), and zipCode ("60654"). Write code to combine them into a single, formatted string that looks exactly like this: Chicago, IL 60654.

    Hint: Remember that you can concatenate strings with other strings, including literals like ", " and " ".

    2. Extract a Username from a URL

    You are given a string representing a social media profile URL: String url = "https://www.social.com/profile/sofia_g". Your task is to extract just the username (sofia_g) from the end of the URL. You can assume the username always comes after the last /.

    Hint: The indexOf method can find the first occurrence of a character. Is there a way to find the last one? (Look up lastIndexOf if you're curious, or think how you could use substring and indexOf together).

    Visualizing address formatting with string concatenation.
    Extracting a username using `lastIndexOf` and `substring`.