Reputation: 1537
This is my code:
String s = "This\tis\tmy\tsmall\texample\tstring\twhich\tI'm\tgoing\tto\tuse\tfor\tpattern\tmatching.";
System.out.println(s);
it printed
This is my small example string which I'm going to use for pattern matching.
How many spaces does the "\t" key occupy? Sometime it occupy one space,sometime it occupy two spaces, Sometime it occupy five spaces.why?Any help would be appreciate.
Upvotes: 0
Views: 235
Reputation: 141
You should use " " instead of "/t".
A tab in a string usually occupies either 4 or 8 column spaces. Here, these column spaces also include the word length. If your word length is less than 4, it would take 4 column spaces otherwise 8 spaces.
Eg- 'The\t' is equivalent to 'The*' and 'small\t' to 'small***'
Upvotes: 0
Reputation: 262504
There is the concept of tab size (usually 8), which is configurable, but that does not directly determine how much visual space a given tab uses.
If you enter a tab, it moves to the next tab position. For example from 3 -> 8. Or from 6 -> 8. It does not move a fixed amount of spaces, it depends on where the "cursor" is at the moment.
In a publishing/layout context, where you have non-proportional fonts, the tab size may not even be a number of characters, but rather a physical width (like an inch).
Upvotes: 1