Using \n in strings

Find quick help here to get you started with Hollywood
Post Reply
amyren
Posts: 361
Joined: Thu May 02, 2019 11:53 am

Using \n in strings

Post 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?
User avatar
msu
Posts: 71
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: Using \n in strings

Post by msu »

Look there: https://www.hollywood-mal.com/docs/html ... tring.html

Code: Select all

string$ = "Line one\\nLine2"
DebugPrint(string$)
amyren
Posts: 361
Joined: Thu May 02, 2019 11:53 am

Re: Using \n in strings

Post 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)[
User avatar
msu
Posts: 71
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: Using \n in strings

Post 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)
User avatar
Juan Carlos
Posts: 889
Joined: Mon Sep 06, 2010 1:02 pm

Re: Using \n in strings

Post by Juan Carlos »

Aldo there is other option
TexOut(10, 10, "Text1".."/n".."Text2")
amyren
Posts: 361
Joined: Thu May 02, 2019 11:53 am

Re: Using \n in strings

Post 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$
Post Reply