File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Length of String when printed Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Length of String when printed" Watch "Length of String when printed" New topic
Author

Length of String when printed

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 147
Hi,

Is there a way to know how long a String will be when it has been printed. For example...



This string has a length of 35, but when you print it in a file it takes up 51. Because the tabs are not interpreted until it is written to a file. How can I know this length when printed?

Thanks in advance for any help.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 8428

tabs can be re-defined by an application. My code editor has tabs equal to 3 spaces, but my word processor has it equal to 8. So I think there isn't a definitive answer to your question.


Never ascribe to malice that which can be adequately explained by stupidity.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3141
Jehan Jaleel wrote:
This string has a length of 35, but when you print it in a file it takes up 51.


No, it has a length of 35 because that is the number of characters in the String. If you use a PrintWriter, BufferedWriter, etc., then Java will write out the 35 chars you give it.

Because the tabs are not interpreted until it is written to a file.


False. Tabs are not expanded until the file is read and its contents displayed in a text editor. (Or until it is read by some other tool that chooses to expand tabs.) And this does not affect the contents of the file. It still contains the 35 chars, including the tab characters. Now, if you save it from your text editor, you might have it configured to save it back as spaces instead of tabs, but again, that is the tool explicitly replacing those tab characters with spaces.

How can I know this length when printed?


You mean how can you know the length when displayed? You can't, because you can't know which tool will be used to display it or how it will be configured.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26720
fred rosenberger wrote: . . . but my word processor has [tab] equal to 8. . . .
And my word processor has tab = ½ inch, which is not equivalent to any particular number of characters at all.
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18370

One way would be to count characters as you loop through the String. Once you encounter a tab you need to treat the counter specially. Assuming you want tabs to fill up to 8 spaces, that would mean adding 1 for the tab itself and then rounding up to the nearest multiple of 8.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 147
Thanks for the responses guys. I did not know that tabs were not read until the file is opened by an editor.

I am using spaces now in my program instead of tabs, this way the formatting should be consistent regardless of the editor.

Thanks again.
 
 
subject: Length of String when printed
 
Threads others viewed
NX : reading the database ( B&S)
JAR FileNotFoundException
Max length for a String?
strange String problem ?
How to print out string at random interval
IntelliJ Java IDE