Page 1 of 1

Using \n in strings

Posted: Mon Apr 13, 2020 11:05 pm
by amyren
If I make a string like this

Code: Select all

string$ = "Line one\nLine2"
Debugprint(string$)
That will come out as two lines

But if I have the exact same text in a file and read the line into a string, it will debugprint as a single line.

Why is this?

Re: Using \n in strings

Posted: Tue Apr 14, 2020 2:20 am
by msu
Look there: https://www.hollywood-mal.com/docs/html ... tring.html

Code: Select all

string$ = "Line one\\nLine2"
DebugPrint(string$)

Re: Using \n in strings

Posted: Tue Apr 14, 2020 7:59 am
by amyren
I was thinking the other way, I did not intend to have that backslash-n written out as text.
I wanted to know why the string read from the file is treated differently.
Like what if I wanted the imported string to come out as two lines?

If I save a file named test.tx containing one string: This is line one\nThis is line two
The example below will display one line in the requestor.

Code: Select all

OpenFile(1, "test.txt", #MODE_READ)
string$ = Readline(1)
CloseFile(1)
SystemRequest("",string$, "OK", #REQICON_INFORMATION)
But this will show two lines:

Code: Select all

string$ =  "This is line one\nThis is line two"
SystemRequest("",string$, "OK", #REQICON_INFORMATION)[

Re: Using \n in strings

Posted: Tue Apr 14, 2020 11:06 am
by msu
Escape sequences should not be used in text files. Better use the appropriate ASCII character.

Here's an example (Please adjust the file path!):

Code: Select all

CRLF = Chr(13)..Chr(10) ;Windows
string$ =  "This is line one"..CRLF.."This is Line two"
SystemRequest("",string$, "OK", #REQICON_INFORMATION)


OpenFile(1, "C:/Users/Micha/Desktop/a.txt", #MODE_READWRITE)
WriteString(1, string$)
CloseFile(1)
OpenFile(1, "C:/Users/Micha/Desktop/a.txt", #MODE_READ)
string$ = ReadString(1)
CloseFile(1)
SystemRequest("",string$, "OK", #REQICON_INFORMATION)

Re: Using \n in strings

Posted: Thu Apr 16, 2020 3:57 pm
by Juan Carlos
Aldo there is other option
TexOut(10, 10, "Text1".."/n".."Text2")

Re: Using \n in strings

Posted: Fri Apr 17, 2020 7:32 pm
by amyren
Thanks for the replies.
The string I want to read is just a part of a larger text file, so I use Readline(1) to read one line at the time. Readstring works differently and I think combining this with Readline might complicate things.

I just do it by using two lines for this in the file, and use Readline to get them.
Then combine the two strings with

Code: Select all

combined$ = string15$.."\n"..string16$