954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String formatting using the "\t" character

Hi All,
I am using a String formatting which separates two strings using the "\t" (tab) character like below:
String _str = "col1" + "\t" + "col2" + "\t" + "col3";

I get the following output:
col1 col2 col3.

this is fine. But because i have a horizontal restriction to abide to. i need to control the tab size or the spaces left blank the "\t". i.e I wish to have col1, col2, col3 closer to each other than what i caused by the tab.

Please help! Urgent issue.

Thanks,
Ashok.

a_iyer20
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

"\t" will automatically take a tab, so if you need less space open, you shouldn't use that. you may try by replacing the "\t" with a couple of spaces, or try formatted output.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Hi, Thanks for your feedback. I have tried with spaces but it doesnt give the desired output. He indentation falls apart in the next rows.

You mentioned trying out formatted output. Could you explain this to me.

Thanks,
Ashok.

a_iyer20
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Well, don't just use spaces on your own.

Try formatting as he suggested. Take a look at
String.format()
to format the String before output or
PrintStream.printf()
to format the String during output (System.out is a PrintStream if it is System.out that you are using).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Hi Masijade,

Could you please help me in understanding the String.format() more closely. I am in urgent need. Could you help by giving some examples.

Thanks,
Ashok.

a_iyer20
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Hi Masijade,

Could you please help me in understanding the String.format() more closely. I am in urgent need. Could you help by giving some examples.

Thanks, Ashok.

the api's are allways a good place to start, otherwise there's allways google .

the intention of this forum is to learn for yourself, with guidance from part of the other users. by making mistakes and have other people looking into it, not by letting other people write you the code you want to have.

look into the api's and check how it works, try writing what you want it to do and if it still does not give you the result you need, post your code and error here.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Aah! I found how it can be done using String.format() but the issue is my company has jdk1.4. So I have to manage within it. Could you please help?

a_iyer20
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Hi Masijade,

Could you please help me in understanding the String.format() more closely. I am in urgent need. Could you help by giving some examples.

Thanks, Ashok.


Read this carefully http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax

And then, as already said, attempt to do it yourself, if you have problems post what you have and we will attempt to help you correct it. And that link contains examples and a complete explanation of the format string to be used in the format method, so it will contain more than enough information for you to be able to accomplish your task.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Oh yes, I understood the way to format using String.format(). It is so convinient but as fate would have had it, my issue is little bigger: I am not using jdk1.5 as I pointed earlier.

Kindly help!

a_iyer20
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Then you're going to have to reproduce the results yourself.

Take a look at the values to be written into each field, find the longest, and pad the rest in that column to match it.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You