Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (TipmyPip, AndrewAMD), 13,824 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 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,440
Red Dwarf
Michael_Schwarz Offline
Senior Expert
Michael_Schwarz  Offline
Senior Expert

Joined: Aug 2003
Posts: 7,440
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 | 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