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

String#repeat

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 11
    • core-libs
    • None
    • minimal
    • New method in a final class.
    • Java API
    • SE

      Summary

      Introduction of a new instance method String::repeat to allow an efficient and concise approach for generating repeated character sequences as strings.

      Problem

      When generating text for human consumption, it is common to format the text to improve readability. Examples; indentation of code blocks, formatting of numerical values or tabularization of data. Formatting requires padding with characters, usually spaces, to align results as required.

      To accomplish this, a developer will typically create a method that will take a character or string and repeat that character or string N times. Historically, the developer will use either string concatenation or StringBuilder to construct the repeating string.

      The problem is that each Java developer has to create this commonly required method or locate a 3rd party library that provides the repeat functionality. Secondarily but importantly, string concatenation and StringBuilder are not the most efficient ways to repeat strings.

      Solution

      The proposal is to introduce a new instance method to the class String that uses intimate knowledge of the String structure to efficiently replicate the String instance. The method takes one integer argument, count indicating the number of times the string is to be repeated.

      String repeat(int count);

      Two techniques will be used to generate repeated strings. In both cases a byte array is allocated of size this.value.length * count. This array will be used as the value of repeating string. If the String instance is a single Latin1 character then the byte array is populated using Arrays::fill. In all other cases, the byte array is populated using a replicating pyramid copy; size = 1, 2, 4, 8, ... 2^log2(N - 1), rest.

      Both these technicals use optimal copying, use no intermediate structures (allocations) and avoid the char to byte conversion used in compact strings.

      This solution does not address repeating in Appendables (ex. StringBuilder) which may be the subject of separate CSR. This solution also does not discuss codepoints or CharSequences which are both easily convert to strings.

      Specification

        /**
       * Returns a string whose value is the concatenation of this
       * string repeated {@code count} times.
       * <p>
       * If count or length is zero then the empty string is returned.
       * <p>
       * This method may be used to create space padding for
       * formatting text or zero padding for formatting numbers.
       * @param   count number of times to repeat
       * @return  A string composed of this string repeated
       *          {@code count} times or the empty string if count
       *          or length is zero.
       * @throws  IllegalArgumentException if the {@code count} is
       *          negative.
       */
      public String repeat(int count) {

            jlaskey Jim Laskey
            jlaskey Jim Laskey
            Alan Bateman, Xueming Shen
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: