2

I came across a weired situation while using tabs. Mostly tabs are made up of 8 or 4 spaces. But lets say right now its 8

For example

"abc"+"\t"+"bcd"      -> abc        bcd
"abcderft"+"\t"+"bcd" -> abcderft   bcd

Why am getting differences in tab distance between two strings ?

Checked this post too

https://stackoverflow.com/questions/6000810/printing-with-t-tabs-does-not-result-in-aligned-columns

Didnt clear much.

Even if tab is equivalent to 8 spaces, then why its considering the string count ? I mean if my string length is 3 then next string am getting after only 5 spaces, and if string length is 8 then next string is coming after 8 characters.

Does tabs includes the string length too post which its being added ?

Maria
  • 452
  • 1
  • 6
  • 20
  • Tabs don't do anything in Java. They are just characters. The way they are printed is a function of your terminal. – Andy Turner Feb 14 '20 at 17:10
  • @AndyTurner, if I do string1+"\t"+string, Will it consider it as a whole string or strings seperated by tab ? – Maria Feb 14 '20 at 17:25
  • Maria, when you concatenate strimgs the result of that expression is a new "whole" string. – Stefan Feb 14 '20 at 18:03
  • @Stefan, Any way I could treat them as a tab delimited string ? – Maria Feb 14 '20 at 18:24
  • You can split strings at tab characters using ```String[] parts=yourString.split("\t")```. If that is not what you want, then please be more specific what input you have and what you want to achieve. – Stefan Feb 14 '20 at 18:30

1 Answers1

1

Tabs are made to align text in columns.
So yes they are multiple spaces from an empty start of the line, but not anymore following some text, as they'll go to say the next columns multiple of 8 (or 4).
So as you guessed, they include part of the length of he text before them

B. Go
  • 1,436
  • 4
  • 15
  • 22