Try this one, if you optimize it, let me know :

UInt16 StrReplace(Char *ioStr, UInt16 inMaxLen, const Char *inParamStr,
const Char *fndParamStr)
{
 Char *found;
 Boolean quit = false;
 UInt16 occurences = 0;
 UInt16 newLength;
 UInt16 l1 = StrLen(fndParamStr);
 UInt16 l2 = 0;
 UInt16 l3 = StrLen(ioStr);
 UInt16 next = 0;

 if (inParamStr)
  l2 = StrLen(inParamStr); // can be null to know how many occur.

 while (((found = StrStr(ioStr+next, fndParamStr)) != NULL) && (!quit))
 {
  occurences++;
  newLength = (StrLen(ioStr) - l1 + l2);

  if ( newLength > inMaxLen )
  {
   quit = true;
   occurences--;
  }
  else if (inParamStr)
  {
   MemMove(found + l2, found + l1, inMaxLen-(found-ioStr+l2));
   MemMove(found, inParamStr, l2);
   next = found - ioStr + l2;
  }
  else
   next = found - ioStr + l1;
 }

 if (inParamStr)
  ioStr[l3 + l2*occurences - l1*occurences] = 0;

 return occurences;
}

----- Original Message ----- 
From: "Jim Duffy" <[EMAIL PROTECTED]>
Newsgroups: palm-dev-forum
To: "Palm Developer Forum" <[EMAIL PROTECTED]>
Sent: Thursday, August 07, 2003 10:22 AM
Subject: Replace Substrings


>
> Hi all,
>
> Does anyone know a way to search through a string for all instances of a
> substring and replace them with a different substring?
>
> For example:
>
> string1 "This[CR]is a [CR] Test"
>
> I need to replace all the "[CR]" with the '\n' newline character..
>
> I've tried using StrStr() and TxtReplaceStr() but these aren't really cut
> out for this.  Can't find any other API that might work..
>
> Thanks for the help..
>
> Jim
>
>
>
> -- 
> For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/
>

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to