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
  • 23,209
  • 4
  • 50
  • 65
dracula90
  • 17
  • 5

2 Answers2

9

The simplest would probably be to use a regex:

String dashes = input.replaceAll(".", "-");
assylias
  • 321,522
  • 82
  • 660
  • 783
1
StringBuilder str = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
    str.append("-");
}
System.out.println(str.toString());
Mason T.
  • 1,567
  • 1
  • 14
  • 33