As a follow-up to our post on Escaping Quotes in Strings, someone asked how they might deal with something such as the tab character in a String.
Unlike with a quote, dealing with a non-visual character (such as a tab or end-of-line) in a String is trickier because you can’t actually type those characters. So instead you have to refer to the character another way.
In the case of end-of-line (EOL), you can use the EndOfLine command in Xojo. By default this gives you the end-of-line character for the platform you are running on. On Windows, an EOL is indicated by ASCII character 13 followed by ASCII character 10, also known as carriage return (CR) and line feed (LF).
Quick Tangent: What on earth does carriage return and line feed mean? The terminology comes from typewriters. On manual typewriters each time you pressed a key on the keyboard an arm would swing up and press the letter onto an ink ribbon and onto the page. The position where the letter appeared on the page was managed by the carriage. It moved from left to right as you typed. When you reached the end, a bell would ding and you’d have to push a lever to return the carriage back to the left side of the page. This was a carriage return. Sometimes you’d want to go back to the beginning of the same line so you could type over existing characters to make them appear bold, but if you pushed the lever a bit harder it would also advance (or feed) the paper up one line. This was a line feed. Originally computers used these same concepts to move a cursor around a character-based text editing screen.
On Mac and Linux, EOL is indicated by character 10 (LF).
So you can generically add an EOL to a String like this:
Var myString As String = "Hello, World!" + EndOfLine
If you want a specific EOL, you can also request it like this:
Var s1 As String = "Hello, World!" + EndOfLine.Windows Var s2 As String = "Hello, World!" + EndOfLine.macOS
But what about non-visual characters that don’t have a built-in command such as tab? For these you’ll have to specify the ASCII value yourself, which in the case of tab is 9. Because this is a standard ASCII non-visual character, one way you can do this using the Chr function (originally from BASIC, but still around in Xojo) or the updated String.ChrByte function:
Var s3 As String = "Hello" + String.ChrByte(9) + "World"
Another option is to create a Unicode literal constant using the &u operator. For this you have to provide the hex value of the number, but fortunately for tab, 9 is the same in both decimal and hex.
Const kTab As String = &u09 Var s4 As String = "Hello" + kTab + "World"
If you want something more global you can even create a method that returns the tab character.