Java String Manipulation: Multiple Approaches to Remove First and Last Characters

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: Java String Manipulation | substring Method | Character Removal

Abstract: This article provides a comprehensive exploration of various techniques for removing the first and last characters from strings in Java. By analyzing the core principles of the substring method with detailed code examples, it delves into character deletion strategies based on index positioning. The paper compares performance differences and applicable scenarios of different methods, extending to alternative solutions using regular expressions and Apache Commons Lang library. For common scenarios where data is wrapped in square brackets in web service responses, complete solutions and best practice recommendations are provided.

Problem Background and Requirements Analysis

In web service development, it is common to handle data formats returned by SOAP messages. As shown in the example, the LoginToken value obtained from the web service is wrapped in square brackets [ and ], and these decorative characters need to be removed for practical use.

Core Solution: substring Method

The substring method based on string indexing is the most direct and effective solution. This method extracts a substring by specifying start and end indices.

String loginToken = "[wdsd34svdf]";
String result = loginToken.substring(1, loginToken.length() - 1);
System.out.println(result); // Output: wdsd34svdf

Code Analysis:

In-depth Method Principle Analysis

The substring method in Java operates based on character arrays with O(1) time complexity. Its internal implementation directly references the specified interval of the original character array, avoiding unnecessary memory copying.

Key points of index calculation:

// Assuming the string is "[token]"
// Indices: 0 1 2 3 4 5 6
// Characters: [ t o k e n ]
// substring(1, 6) → "token"

Boundary Case Handling

Various boundary scenarios need consideration in practical applications:

// Empty string handling
if (loginToken != null && loginToken.length() > 2) {
    result = loginToken.substring(1, loginToken.length() - 1);
} else {
    result = loginToken;
}

// Dynamic character detection
if (loginToken.startsWith("[") && loginToken.endsWith("]")) {
    result = loginToken.substring(1, loginToken.length() - 1);
}

Alternative Approach Comparison

Besides the substring method, other viable technical solutions exist:

Regular Expression Method

String result = loginToken.replaceAll("^\\[|\\]$", "");

Apache Commons Lang

import org.apache.commons.lang3.StringUtils;
String result = StringUtils.substring(loginToken, 1, -1);

Performance Optimization Recommendations

In performance-sensitive scenarios:

Practical Application Extensions

This technique can be extended to other similar scenarios:

By systematically mastering string manipulation methods, data processing efficiency and code quality can be significantly enhanced.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.