Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
3 registered members (AndrewAMD, Grant, Neb), 908 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
String search and replace, and fix \n linefeeds #68108
03/24/06 20:35
03/24/06 20:35
Joined: Jul 2005
Posts: 366
eleroux Offline OP
Senior Member
eleroux  Offline OP
Senior Member

Joined: Jul 2005
Posts: 366
I had a problem reading multi-line strings (with \n line feeds) from text files using file_str_read. The solution produced a small function I decided to share, because it may be useful for other matters.


1) A search and replace function

Code:

STRING clipstr;
function searchAndReplace(str,searchstr,replacestr)
{
str_cpy(clipstr,str);
var foundat;
foundat = str_stri(clipstr,searchstr);

while(foundat)
{
str_trunc(str,str_len(clipstr)-foundat+1);
str_cat(str,replacestr);
str_clip(clipstr,foundat+str_len(searchstr)-1);
str_cat(str,clipstr);
foundat = str_stri(clipstr,searchstr);
}

}


Example:
mystring="could not read the file";
searchAndReplace(mystring,"read","LOAD");
//mystring now is: "could not LOAD the file";






2) The 'special characters' trick

Code:

// ...somewhere in your text loading function
file_str_read(mystring);
searchAndReplace(mystring,"\\n","\n"); //replaces "\n" with linefeeds in the string
text.string[0] = mystring;




Explanation: special characters are converted directly in C-Script. When you code:
STRING mystring = "Multi \n Line";
You are not putting a real '\n' in the string. The engine replaces the \n for a linefeed.

The same works inverse: if your text file contains "Multi \n Line", the engine doesn't see a linefeed, but a backslash and a 'n'. Thus, you must scan the text looking for a backslash and a 'n' and replacing that by a linefeed.

The special character for backslash is "\\".
So, if you search for "\\n" and replace it by "\n", you are asking "search the text for a backslash followed by 'n' and replace them by a linefeed".

Re: String search and replace, and fix \n linefeed [Re: eleroux] #68109
03/24/06 21:13
03/24/06 21:13
Joined: Aug 2003
Posts: 7,439
Red Dwarf
Michael_Schwarz Offline
Senior Expert
Michael_Schwarz  Offline
Senior Expert

Joined: Aug 2003
Posts: 7,439
Red Dwarf
Now I did think for long time about how I could do that, lol.
A little shame for me, being a good programmer and didnt figure that out, anyways, thanks for sharing this, its extremely useful!


"Sometimes JCL reminds me of Notch, but more competent" ~ Kiyaku

Moderated by  adoado, checkbutton, mk_1, Perro 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1