This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to format the string using string builder in java

 
prajna pa
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need the string to be formatted and it should hava 8 characters only

if the string is greater than the length it should take only 8 characters.

if the string is lesser than the length it should fill the remaining with empty spaces

and also all the string should be right aligned

thanks in advance

[size=12]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the API documentation of class java.lang.String - it contains many methods which are useful to do what you want.

Try writing some code to solve this yourself. If you get stuck, show us your code and explain where you are stuck.
 
prajna pa
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here i am writtting my code

i need the string to be right aligned and should have the fixed length of 8 characters and also less than fixed should fill with empty spaces......
Now i had given 'p' as the character to fill i need empty space for this my code is not working
so please help me

Thank you in advance
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Have a look at the API documentation of class java.lang.String - it contains many methods which are useful to do what you want.


A further hint: String.format is very useful.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have some style problems in that code, too, I am afraid. Names like p and c for characters and Strings are difficult to follow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic