How do i split string to lines?

Monkey Forums/Monkey Programming/How do i split string to lines?

hardcoal(Posted 2011) [#1]
Let say I have

original:
This is my string and I want it to be in two lines.

Splited:
This is my string
and I want it to be in two lines.

is there a charecter which represents Enter?


;---------------------------;

I know this code...

Function PrintTextFileSimple(TextString$)
For Local line$=Eachin str.Split( "~n" )
Print "line="+line
Next
End Function

but this is when I load a text file

but when I want to create a Text File from a string
I dont know how to go one line down every time i want.


Aman(Posted 2011) [#2]
This is what represents a new line: ~n

This is my string~nand I want it to be in two lines.

The function you use actually load a string not a file. You are giving the function the file in the form of a string.

The first line inside the function assign a part of the string to local line that ends when it finds the new line character ~n

if you change this:
For Local line$=Eachin str.Split( "~n" )

to this:
For Local line$=Eachin str.Split( " " )

the result will be printing each word in a new line


hardcoal(Posted 2011) [#3]
The Printing is not the problem for me.
Im trying to save a text file so when i open it externaly via notepad
i will see it splited into lines and not as a one line.

is it possible at all in monkey?

in blitz3d i used to do WriteLine command.
but here you dont have this option.

by the way im using GLFW Target


Raz(Posted 2011) [#4]
When outputting the file, split each line with "~n"

Local output:String = "Line 1~nLine2~nLine3~n"
SaveString( output, "somefile.txt" )



hardcoal(Posted 2011) [#5]
did you try it?
because i have, and it didnt work.
im starting to think its impossible in monkey.
hope im wrong


Perturbatio(Posted 2011) [#6]
If you're opening it in notepad on windows, you will probably need to do a CRLF (~r~n) since windows needs two characters to represent a new line.

Alternatively, use notepad++


charlie(Posted 2011) [#7]
Surely adding the newline character manually is a bad idea? Especially if you are loading it in from a file.

Personally, i'd be more inclined to work out how many times longer the string is that the horizontal area i'm trying to draw to, and split it at the previous space character.

Cheers
Charlie


ziggy(Posted 2011) [#8]
If working on windows, open the file in wordpad instead of notepad. Wordpad understand all kind of new line combinations, while notepad does not.

Mainly there are this "new line" combinations:

CR+LF: Very usual in Windows and most other early non-Unix and non-IBM OSes, CP/M, MP/M, DOS (MS-DOS, PC-DOS, etc.), (Also on Atari TOS, OS/2, Symbian OS, Palm OS)

LF+CR: Acorn BBC spooled text output. Not very usual, but you can find it.

CR: Commodore 8-bit machines, Acorn BBC, TRS-80, Apple II family, Mac OS up to version 9 and OS-9. Very usual on non windows 8 bits string encoded text files.

LF: Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.). Also Amiga, RISC OS, and others use this format

RS: QNX pre-POSIX implementation.

I'm not sure, but I think the ~n scape char in Monkey just introduces the CR character. This can be confusing notepad.


hardcoal(Posted 2011) [#9]
Thanks Zig the ~r~n did the job! thats exactly what i wanted
thanks all for the help

this notepad++ is amazing althow I want to make my own notepad some day
with some elements of my own ideas.

ok sleeping time.. cheers