Reputation: 3048
I need to write currency values like $35.40 (thirty five dollars and forty cents)
and after that, i want to write some "****"
so at the end it will be:
thirty five dollars and forty cents*********
in a maximun of 100 characters
I've asked a question about something very likely but I couldn't understand the main command.
String format = String.format("%%-%ds", 100);
String valorPorExtenso = String.format(format, new Extenso(tituloTO.getValor()).toString());
What do I need to change on format to put ***
at the end of my sentence?
The way it is now it puts spaces.
Upvotes: 3
Views: 369
Reputation: 5563
This formatting will give you 100 characters including your string
String.format("%-100s", "thirty five dollars and forty cents.").replaceAll(" ", "**");
UPDATE :
String.format("%-100s", "thirty five dollars and forty cents.").replaceAll(" ", "**").replace("* ", "**");
Upvotes: 1
Reputation: 44193
Short answer, you can't pad with anything other than spaces using String.format. Either use apache StringUtils or write a snippet of code to do it yourself, there is an answer here http://www.rgagnon.com/javadetails/java-0448.html
Upvotes: 1
Reputation: 8560
I would do:
String line = "thirty five dollars and forty cents";
StringBuilder lineBuffer = new StringBuilder(line);
for(int i=0; i < 100 - line.length(); i++){
lineBuffer.append("*");
}
line = lineBuffer.toString();
Upvotes: 2
Reputation: 9777
You may want to look at commons-lang http://commons.apache.org/lang/api-release/index.html
I'm under the impression that you want to pad this out to 100 characters.
The other option is to create a base string of 100 '*' characters.
Create a string builder and do the following:
StringBuilder sb= new StringBuilder(new Extenso(tituloTO.getValor()).toString());
sb.append(STARS_STR);
String val= sb.substring(0, 100);
That should get out the value formatted out.
Upvotes: 2
Reputation: 3118
You can't with String.format(), there are however utils to do it in apache's StringUtils. Or just write little method that append the * for your specified length.
Upvotes: 0