aspose file tools*
The moose likes Beginning Java and the fly likes How to format the string using string builder in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to format the string using string builder in java" Watch "How to format the string using string builder in java" New topic
Author

How to format the string using string builder in java

prajna pa
Ranch Hand

Joined: Oct 04, 2012
Posts: 36
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

Joined: Aug 16, 2005
Posts: 14144
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
prajna pa
Ranch Hand

Joined: Oct 04, 2012
Posts: 36
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

Joined: May 22, 2012
Posts: 867
    
    5
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

Joined: Oct 13, 2005
Posts: 38784
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to format the string using string builder in java