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

(str) n times repetition of character constructor for java.lang.String

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core-libs

      Description

      A DESCRIPTION OF THE REQUEST :
      In C++ (ISO/IEC 14882) I can simply use this constructor (http://www.cplusplus.com/reference/string/string/string/):

      string myString(25, '*');

      to make myString value "***...**" (25 times).

      I don't understand why this was not included in java.lang.String class. Obviously I can use e.g.:

      StringBuilder myStringBuilder = new StringBuilder();
         for(int i = 0; i < 25; i++)
            gwiazdkiBuilder.append('*');
      String myString = myStringBuilder.toString();

      But why it can't be done simpler like this:
      String myString = new myString(25, '*');

      JUSTIFICATION :
      C++ have it, even C# have:

      string myString = new string('*', 25);
      Console.WriteLine(myString);

      It was suprise for me that java String don't have this constructor.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Adding "n times repetition of character constructor" to java.lang.String in JDK 7.
      ACTUAL -
      There is no such constructor (even Visual Basic have !)

      ---------- BEGIN SOURCE ----------
      StringBuilder myStringBuilder = new StringBuilder();
         for(int i = 0; i < 25; i++)
            gwiazdkiBuilder.append('*');
      String myString = myStringBuilder.toString();

      // String myString = new String(25, '*'); or String myString = new String('*', 25);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      StringBuilder myStringBuilder = new StringBuilder();
         for(int i = 0; i < 25; i++)
            gwiazdkiBuilder.append('*');
      String myString = myStringBuilder.toString();

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: