1

So I get a random word from a website and use the length() method to obtain how many letters are in the word. Now I want to display these as dashes (-). For example, say the word is "dogs". The length method will return 4. Now I want to convert that into ----. Is there any way in java to say in - the number representing the length of the word?

helderdarocha
  • 22,383
  • 4
  • 42
  • 61
dracula90
  • 17
  • 5

2 Answers2

9

The simplest would probably be to use a regex:

String dashes = input.replaceAll(".", "-");
assylias
  • 310,138
  • 72
  • 642
  • 762
1
StringBuilder str = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
    str.append("-");
}
System.out.println(str.toString());
Mason T.
  • 1,555
  • 1
  • 14
  • 32