Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Length of String when printed

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic