Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / [SOLVED] How to split a string by linebreak?

Author
Message
Supertino
7
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 16th Jul 2018 14:44 Edited at: 16th Jul 2018 14:57
This post has been marked by the post author as the answer.
Lets say I create a string with line breaks


Now say I now want to split that string by line break and save the 3 parts as String[]


Lets ignore the method of splitting the string what I am struggling with the a way to detect where the line breaks are, the linebreak character\sequence, I've tried looking for \n, \r, \n\r, ~n, ~r, Char(10) and so on.

Any one have some advice.

The author of this post has marked a post as an answer.

Go to answer

Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 16th Jul 2018 15:06 Edited at: 16th Jul 2018 15:40
Firstly, you could load up each string separately using ReadLine() as you load the file so that the whole file isnt put into one single string. Instead its put into separate strings as its loaded


If you already have it as one string (fullfile$) then you can use GetStringToken() or GetStringToken2()

delimiters$ = chr(10) + chr(13) // these delimiters are line feed and carriage return
text$[0] = GetStringToken(fullfile$,delimiters$,1)
text$[1] = GetStringToken(fullfile$,delimiters$,2)
text$[2] = GetStringToken(fullfile$,delimiters$,3)

This works fine on a windows txt file

CountStringTokens(0 would tell you how many lines you could split it up into

EDIT: arghhh...you changed your original post halfway through me answering!!

With your newly edited version of the original post....just a delimiter of chr(10) would work fine.
Ie.... delimiters$ = chr(10)


So if you just want the position of your chr(10) then you can use
integer FindString( Fullfile$, delimiter$, ignoreCase, start )

then when you have found the first position of a chr(10) you put that +1 into the start value for the second search to find the next chr(10)..... and so on
Supertino
7
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 18th Jul 2018 11:12
Thanks Bengismo

I am sure I tried Chr(10), then I think of it I use my own function to split a string and not specifiy the built in GetStringToken() so that might be where I am going wrong. I'll give that a try tonight.

Login to post a reply

Server time is: 2024-11-23 12:34:14
Your offset time is: 2024-11-23 12:34:14