Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6717200

(str) String.left(int) and String.right(int) convenience methods

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • 6
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      The String class should provide convenience method left(int) and right(int) that return a String built from the left n or right n characters of the original String.

      JUSTIFICATION :
      left(int) and right(int) exist in many other programming languages like BASIC, SQL, etc. and are often needed. Certainly one can use substring(int,int) to work around, but it is just annoying that there is no left and right:
      * It would be much easier to read and understand the code.
      * It would be less error prone than dealing with the index values of substring.
      * You could do "String.format(x, y).right(n)" in one single line, while with substring you need two lines, what is more to write, harder to read, and likely for errors.
      * The implementation in the JRE is fairly easy.
      * There is no good reason against it.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      s.left(n) will return s.substring(0, n)

      s.right(n) will return s.substring(s.length() - n)

      ACTUAL -
        Programmer must work around:

      If s.left() is needed, he must write s.substring(0, n).

      If s.right() is needed, he needs to write s.substring(s.length() - n) if s is a variable, or he must write a second line if s is a function that shall not get called a second time.

      ---------- BEGIN SOURCE ----------
      If you really need a test case for THIS proposal, then please write to my email address.
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      If s.left() is needed, must write s.substring(0, n).

      If s.right() is needed, need to write s.substring(s.length() - n) if s is a variable, or must write a second line if s is a function that shall not get called a second time.

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: