Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/05/23 14:22
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
1 registered members (AndrewAMD), 599 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 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