Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:18:35+00:00 2026-06-17T08:18:35+00:00

So, my question has been asked by someone else in it’s Java form here:

  • 0

So, my question has been asked by someone else in it’s Java form here: Java – Create a new String instance with specified length and filled with specific character. Best solution?

. . . but I’m looking for its JavaScript equivalent.

Basically, I’m wanting to dynamically fill text fields with “#” characters, based on the “maxlength” attribute of each field. So, if an input has has maxlength="3", then the field would be filled with “###”.

Ideally there would be something like the Java StringUtils.repeat("#", 10);, but, so far, the best option that I can think of is to loop through and append the “#” characters, one at a time, until the max length is reached. I can’t shake the feeling that there is a more efficient way to do it than that.

Any ideas?

FYI – I can’t simply set a default value in the input, because the “#” characters need to clear on focus, and, if the user didn’t enter a value, will need to be “refilled” on blur. It’s the “refill” step that I’m concerned with

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:18:36+00:00Added an answer on June 17, 2026 at 8:18 am

    The best way to do this (that I’ve seen) is

    var str = new Array(len + 1).join( character );
    

    That creates an array with the given length, and then joins it with the given string to repeat. The .join() function honors the array length regardless of whether the elements have values assigned, and undefined values are rendered as empty strings.

    You have to add 1 to the desired length because the separator string goes between the array elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A similar question has been asked before by someone else , but there were
This question has been asked in a C++ context but I'm curious about Java.
I know this question has been asked a million of times here at stackoverflow,
Sorry if this question has all ready been asked, maybe someone can point me
I realize that this question has been asked and has been answered here but
This question has been asked before (see here and here , for example). However,
Anyone knows how to create one? I know this question has been asked before
It looks like this question has been asked before, here and in other places
I know this question has been asked and answered here on StackOverflow.. but I
Yes, I know this question has been asked 1000 times before... here is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.