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:
substring(1, loginToken.length() - 1)starts extraction from index 1- Index 1 corresponds to the second character, skipping the initial
[ - The end index is
length() - 1, excluding the final] - Java string indices start at 0,
length()returns the string length
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:
- The
substringmethod offers optimal performance, suitable for high-frequency calls - Regular expressions are appropriate for complex pattern matching but incur significant performance overhead
- Third-party libraries provide additional functionality but increase dependency costs
Practical Application Extensions
This technique can be extended to other similar scenarios:
- Processing quote-wrapped JSON strings
- Cleaning XML tag content
- Removing special characters from CSV data
By systematically mastering string manipulation methods, data processing efficiency and code quality can be significantly enhanced.