Jump to content

myReplaceString


Domitan Redenblack
 Share

You are about to reply to a thread that has been inactive for 4761 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

 

Using parse to List as a way to replace strings:
string myReplaceString (string str, string pat, string rep) {
    llSay(0, "myReplaceString: " + str + ", " + pat + ", " + rep);
    list tlTemp = llParseString2List(str, [pat], []);
    llSay(0, (string)tlTemp);
    string tsOut = llDumpList2String(tlTemp, rep);
    llSay(0, "-->" + tsOut);
    return tsOut;
}

 Does not seem to work; I must be missing something obvious here... Help please.

llSay Reports:

   myReplaceString: Info for <date>, <date>, 2011-04-22
   Info for 
    -->Info for 

 

Link to comment
Share on other sites

Okay, I got it. The <date> is the last part of the source string, so the List created only has one element.

If <date> starts or ends the string, this does not work. Recommended solutions?

1. bracket the source string with "." at begin and end, then strip them before returning tsOut ? What if "." is part of the pattern to replace?

2. what else?

ty

 

UPDATE: bracket the source string with an "unlikely" string should work, e.g. bracket with "$#%@=" etc then strip off those before returning result.

 

 

Link to comment
Share on other sites

You're question is a bit cryptic - I will nonetheless try to answer:

You're talking of a source string - I can only guess you're referring to the variable str in your function. This omnious source string in your tests is: Info for <date> and you want to replace the <date> bit by a different string - in your example: 2011-04-22.

If these assumptions are right, you have give most of the answer already: you are parsing the string into a lit - and <date> defines the separtor of the list - plus: empty entries are ignored by the function you are using. So, all you got to do is keep the nulls:

 

myReplaceString (string str, string pat, string rep) {
    llSay(0, llDumpList2String(llParseStringKeepNulls(str, [pat], []), rep));
}

 

 

Link to comment
Share on other sites

almost but not quite... if the search string is back to back in the middle of a larger string llParseString2List will dump the second instance, but llParseStringKeepNulls will insert an extra (when used with llDumbpList2String). what you really need to do is loop through and check each element returned by llParseStringKeepNulls spitting out the replacement for each element that does not have any text

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 4761 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...