Results 1 to 9 of 9
Like Tree4Likes
  • 1 Post By jim829
  • 3 Post By gozzy

Thread: Returning a specified number of spaces or other character

  1. #1
    JonWayn is offline Member
    Join Date
    Mar 2017
    Posts
    5
    Rep Power
    0

    Default Returning a specified number of spaces or other character

    In VB there is a Space function which takes 1 numeric argument and returns a string of spaces that length, and there is a String function that takes additionally a char argument and returns a string made up of that char of the specified length. What is java's equivalent to this function? I know I can roll my own in a for loop but why bother if its already done for me?

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Returning a specified number of spaces or other character

    I've never heard of that type of builtin method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JonWayn is offline Member
    Join Date
    Mar 2017
    Posts
    5
    Rep Power
    0

    Default Re: Returning a specified number of spaces or other character

    I guess for something like this, in java you just have to roll your own. Not that it is difficult I just thought all programming languages would have had an equivalent

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Returning a specified number of spaces or other character

    Before you start coding String-concatenating loops, this will do it.
    Java Code:
      String repeat(char c, int count) {
        StringBuilder sb = new StringBuilder(count);
        sb.setLength(count);
        return sb.toString().replace((char) 0, c);
      }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Returning a specified number of spaces or other character

    I hadn't thought of that. Once I saw what you did I figured you could also do the following:

    Java Code:
    public static String repeat(char c, int count) {
          String s = new String(new byte[count]);
          return s.replace((char)0, c);
    }
    But if I were going to do it I would create an instance of StringBuilder
    and just use the append feature in a loop. The replace method uses
    a loop but it also does a test on each character which isn't really
    necessary.

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Returning a specified number of spaces or other character

    Closest thing in Java would probably be

    Java Code:
    char[] c = new char[numSpaces];
    java.util.Arrays.fill(c, ' ');
    return new String(c);
    Norm, DarrylBurke and jim829 like this.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Returning a specified number of spaces or other character

    This could really be versatile if the API writers of Arrays had also returned the passed array instead of making the return type void. Then you could do something like this:

    Java Code:
    int n = 5;
    int c = 'r';
    String s = new String(Arrays.fill(new char[n], c));
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Returning a specified number of spaces or other character

    This could really be versatile if the API writers of Arrays had also returned the passed array
    Yes, that was dumb not to ;)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Returning a specified number of spaces or other character

    It would be a non-breaking change, at least, if they did decide to do it that way.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 02-07-2014, 09:31 PM
  2. Replies: 14
    Last Post: 10-12-2013, 09:40 AM
  3. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  4. Replies: 9
    Last Post: 03-06-2011, 03:00 AM
  5. Replies: 2
    Last Post: 12-10-2009, 12:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •