Search Authority

Remove Last Character from String in Java: Simple Guide

Removing the last character from a Java String is a common need when normalizing user input, trimming trailing delimiters, or cleaning up formatted output. Because Strings are i...

Mara Ellison
Remove Last Character from String in Java: Simple Guide

Removing the last character from a Java String is a common need when normalizing user input, trimming trailing delimiters, or cleaning up formatted output. Because Strings are immutable in Java, you cannot simply delete a character; instead you create a new String that excludes the unwanted character.

This article shows practical, production-ready ways to remove the last character, compares custom logic versus library methods, and highlights pitfalls such as empty Strings and multibyte text.

Approach Code Pattern Use Case Performance Notes
substring str.substring(0, str.length() - 1) Simple removal when length > 0 O(1) in most JVMs, no copy unless the String is later modified
StringBuilder new StringBuilder(str).deleteCharAt(str.length() - 1).toString() Repeated edits in a loop or builder pipeline Copy occurs only when trimming; efficient for multiple operations
StringUtils.chop StringUtils.chop(str) Apache projects and legacy codebases Slight overhead from null/length checks; consistent behavior
Regular Expression str.replaceAll(".$", "") Removing last character only when it matches a pattern More overhead; prefer explicit length checks for simple trim

Validate Length Before Removing Last Character

Always verify that the String length is greater than zero before subtracting from length. Accessing index length() - 1 on an empty or null input throws StringIndexOutOfBoundsException and can crash your service.

Defensive code should return the original reference for empty input or apply a guard clause if null is possible. This keeps behavior predictable and avoids unexpected exceptions in production.

Prefer Substring for Simple Trimming

Why substring is idiomatic

The substring approach is concise and clearly expresses intent. It avoids object creation overhead when you only need to drop the last character from a standard String.

Handling edge lengths

Combine substring with an explicit length check so that single-character input does not turn into an empty String unintentionally. Decide whether a one-character payload should become empty or remain unchanged based on your domain rules.

Use StringBuilder for Repeated Operations

When to choose a builder

If your code builds, modifies, and trashes multiple String values in a loop, StringBuilder reduces intermediate garbage and can improve throughput.

Chaining methods safely

Using deleteCharAt on a builder preserves readability and lets you mix insert, replace, and delete in a single block, while still ensuring the final conversion to String is explicit.

Leverage Apache Commons StringUtils.chop

Behavior and compatibility

StringUtils.chop returns the String minus its last character, and returns an empty String when the input length is one. It returns null when the input is null, which can simplify legacy integrations but requires careful null checks.

Migration to modern APIs

In new projects, consider replacing StringUtils.chop with explicit substring logic or utility methods you control to reduce external dependency and clarify error handling for empty inputs.

Best Practices for Safe String Trimming in Java

  • Always validate length before accessing index length() - 1.
  • Decide whether a single-character input should become empty or remain unchanged.
  • Use substring for simple, one-time removal to keep code minimal.
  • Choose StringBuilder when building or modifying Strings in loops.
  • Standardize null handling across your codebase to avoid inconsistent behavior.

FAQ

Reader questions

How do I safely remove the last character when the String might be empty?

Check str != null && str.length() > 0 before subtracting one from length. If the condition fails, return str as-is or an empty String, depending on your desired behavior.

What happens with trailing newline or whitespace characters?

Remove the last character exactly as stored; if you want to trim line breaks or spaces first, call str.trim() or str.strip() before applying length-based removal.

Can I use negative indices or patterns to remove the last character?

Negative indices are invalid and will throw an exception. Pattern-based replacement such as replaceAll(".$", "") works but is heavier; reserve it for cases where the last character must match a rule.

Is substring or StringBuilder faster for single removals?

Substring is typically faster for one-off edits because it avoids builder allocation. Use substring unless you are already working inside a builder pipeline.

Related Reading

More pages in this topic cluster.

Who Designed the Nike Logo? The Story Behind the Swoosh

The Nike swoosh is one of the most recognizable symbols in the world, but few people know the story behind its creation. This piece explores who designed the Nike logo, why it h...

Read next
What is the World's Hottest Pepper? 🌶️🔥

When people ask about the world's hottest pepper, they usually mean the variety that currently holds the Guinness World Record and pushes the boundaries of capsaicin heat. Peppe...

Read next
Jon Huertas in This Is Us:角色, 出演时期与剧情影响详解

Jon Huertas 在《这就是我们》中饰演成年 Kevin Pearson,这一角色从2016年首播持续至2022年最终季,构成了剧集核心家庭叙事的重要组成部�...

Read next