String methods (charAt(), concat(), equals(), indexOf(), isEmpty(), join(), lastIndexOf(), length(), split(), substring(), trim())

Eleven String methods, one messy typed title: inspect with charAt/indexOf/length, produce new strings with trim/substring/split/concat/join, and remember substring's end index is exclusive.

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The librarian types like a human

Real input from the front desk this week:

" let us c " (stray spaces), "" (Enter pressed on nothing), "Programming in ANSI C by Balagurusamy" (needs just the title part).

BookBridge must clean, test, slice and search all of it. Java's String class ships a toolbox for exactly this, and your syllabus names 11 tools. Learn them as a family with 2 jobs: methods that inspect a string, and methods that produce a new one (never forgetting: immutable, always new).

Theory

First, learn to count like Java

Every method below speaks in indexes, starting at 0. Pin this example:

"Let Us C"

L e t ␣ U s ␣ C

0 1 2 3 4 5 6 7

So length() is 8 (the count), but the last index is 7 (count minus 1). charAt(4) is 'U'. Mixing up count and last index causes the StringIndexOutOfBoundsException half your batch will meet this week.

At a glance

The inspectors: ask, never change

MethodAnswersOn "Let Us C"
length()How many characters?8
charAt(4)Which char sits at index 4?'U'
indexOf("s")First index of this text, or -15
lastIndexOf(" ")Last index of this text, or -16
isEmpty()Is length() == 0?false
equals(s)Same characters? (last lesson)content check

At a glance

The producers: every result is a NEW string

MethodProducesExample
trim()Copy without leading/trailing spaces" let us c ".trim() → "let us c"
substring(4)From index 4 to the end"Let Us C" → "Us C"
substring(0, 3)Indexes 0,1,2: end EXCLUSIVE"Let Us C" → "Let"
concat(s)This string + s joined"Book".concat("Bridge") → "BookBridge"
split(" ")String[] of the pieces"Let Us C" → {"Let","Us","C"}
String.join("-", parts)Pieces glued with a separator (static)join("-","a","b") → "a-b"

Practical

Cleaning one messy entry, end to end

public class CleanTitle {
    public static void main(String[] args) {
        String raw = "  let us c  ";

        String t = raw.trim();                 // "let us c"
        System.out.println(t.length());        // 8
        System.out.println(t.isEmpty());       // false
        System.out.println(t.indexOf("us"));   // 4
        System.out.println(t.charAt(0));       // l

        String[] words = t.split(" ");         // {"let","us","c"}
        System.out.println(words.length);      // 3  (field, no brackets!)
        System.out.println(String.join("-", words)); // let-us-c
    }
}

Quiz

What does "Let Us C".substring(4, 6) return?

  1. "Us"
  2. "Us "
  3. " Us"
  4. "s C"
Show the answer

"Us"

substring(begin, end) takes indexes begin up to end EXCLUSIVE: indexes 4 and 5, the characters 'U' and 's', so "Us". Option B includes index 6 (the space), the classic off-by-one for students who read end as inclusive. A quick self-check that examiners themselves use: the result's length is always end minus begin, here 6 - 4 = 2 characters. If your answer has the wrong length, re-read the indexes.

Think first

The -1 sentinel

t.indexOf("Python") when t is "let us c": there is no Python anywhere in it. Before tapping: what comes back, and why that particular value?

Show the answer

-1. It cannot be 0 or any positive number, because every one of those is a real index where a match COULD sit; -1 is the one integer that can never be a position, so it safely means "not found". The working idiom everywhere in BookBridge: if (t.indexOf(q) != -1) { found }. Forgetting the check and using the -1 as an index in substring or charAt is an instant runtime exception.

Watch out

Three habits that lose marks

length() vs length: on a String it is a method with brackets, on an array a field without: t.length() but words.length. Exams plant both in one snippet, exactly like our listing.

Forgetting results are new strings: t.trim(); alone cleans nothing; assign it: t = t.trim();

charAt(t.length()) asks for index 8 of an 8-character string: StringIndexOutOfBoundsException. The last character lives at length() - 1.

Theory

Compose them like plumbing

The methods chain, because each producer returns a String ready for the next call: raw.trim().substring(0, 3).equals("let") reads as clean, slice, test. That composability is why these 11 cover most text work BookBridge will ever need. When the job becomes heavy repeated EDITING of one growing text, chaining new-string producers gets wasteful, and that is precisely the cue for the next lesson's StringBuffer.

Summary

Key takeaways

  • Indexes start at 0; length() counts characters, last index is length() - 1.
  • Inspectors: length, charAt, indexOf, lastIndexOf, isEmpty, equals.
  • Producers (always a NEW string): trim, substring, concat, split, and static String.join.
  • substring(begin, end) is end-exclusive; result length = end - begin.
  • indexOf/lastIndexOf return -1 for not found: check before using.
  • String length() has brackets; array length does not.
  • Memory hook: inspectors ask, producers hand you a fresh copy.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Basic Concepts of Strings and Exceptions

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

String methods (charAt(), concat(), equals(), indexOf(), isEmpty(), join(), lastIndexOf(), length(), split(), substring(), trim()) · Java Programming Language · Gri-Learn