JDK-6219457 : (str) String.trim() to trim only at the beginning or at the end
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0u1
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2005-01-20
  • Updated: 2018-04-25
  • Resolved: 2018-04-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11
11Resolved
Related Reports
Duplicate :  
Description
The java.lang.String API should provide an additional trim method with one parameter:

public String trim(int where)

for being able to trim Strings just only at the beginning or just only at the end. The parameter "where" should be one of the following constants: LEADING, TRAILING, BOTH. 

Actually there is only the trim() method, which returns a String with leading and trailing whitespace omitted.

###@###.### 2005-1-20 11:41:14 GMT

Comments
Assigning to Jim Laskey, as this issue might either be fixed or superseded by other string API work currently in progress. Jim, close or handle otherwise as you see fit.
15-03-2018

SUGGESTED FIX I suggest to add both ease-of-use constants as well as a parameterized trim method to the java.lang.String API: /** * Ease-of-use constants for <code>trim()</code>. */ public static final int LEADING = 0; public static final int TRAILING = 1; public static final int BOTH = 2; /** * Returns a copy of the string, with leading, trailing or both whitespace omitted. * @param where * One of the following constants defined in String: * LEADING, TRAILING or BOTH * * @return A copy of this string with leading, trailing or both white * space removed, or this string if it has no leading or * trailing white spaces. */ public String trim(int where) { int len = count; int st = 0; int off = offset; /* avoid getfield opcode */ char[] val = value; /* avoid getfield opcode */ if (where==BOTH || where==LEADING) { while ((st < len) && (val[off + st] <= ' ')) { st++; } } if (where==BOTH || where==TRAILING) { while ((st < len) && (val[off + len - 1] <= ' ')) { len--; } } return ((st > 0) || (len < count)) ? substring(st, len) : this; } public String trim() { return trim(BOTH); } ###@###.### 2005-1-20 11:41:14 GMT
20-01-2005