aspose 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
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: 196
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: 11406
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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: 39395
    
  28
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
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Length of String when printed