On Fri, 29 Apr 2005 13:09:29 -0500, Logan Shaw
<[EMAIL PROTECTED]> wrote:
>Oliver Ott wrote:
>> I searched through the Reference especially the String Manager
>> chapter to find a function I used before with MS VBA.
>>
>> Syntax
>> Replace(expression, find, replace[, start[, count[, compare]]])
>>
>> This function for Visual Basic replaces a char by another char.
>
>Doesn't exist in Palm OS. However, you can use a loop to do it:
>
> void strreplace (Char *str, Char old, Char replacement)
> {
> Char *c;
>
> for (c = str; *c != 0; c++)
> {
> if (*c == old)
> {
> *c = replacement;
> }
> }
> }
>
>That will only replace one character with another, not a whole set.
>
Well that is very interesting!
Did you write the code just for me??
>> Syntax
>> Split(expression[, delimiter[, limit[, compare]]])
>
>The usual way to do this in C is to use strtok(). However, I don't
>think that the Palm has an equivalent for that function. So, you'll
>have to do that manually, too.
>
>If you want to do it strtok() style, strtok() works by taking
>advantage of the fact that C strings end with a null (zero) character.
>So, you can split a string by writing a null character into the
>middle of it, then taking the pointer to the spot after you write
>the null (assuming there is such a spot).
>
>For example, "12.34" is actually stored like this (with made-up
>memory addresses to make the pointer stuff clearer):
>
> "1" "2" "." "3" "4" null
> 5000 5001 5002 5003 5004 5005
>
>You can split it by writing a NULL into location 5002 and taking
>a pointer to location 5003:
>
> "1" "2" null "3" "4" null
> 5000 5001 5002 5003 5004 5005
>
>Thus, you can write a function to find the first delimiter
>character, replace it with a null, and then return a pointer
>to the stuff after it. You can call this function repeatedly
>to process each delimiter character and turn it into a null.
>For example:
>
> Char* replacedelimiter (Char *targetstr, Char delimiter)
> {
> Char *delimiterlocation;
>
> for (delimiterlocation = targetstr;
> *delimiterlocation != 0;
> delimiterlocation++)
> {
> if (*delimiterlocation == delimiter)
> {
> /* change delimiter to 0 */
> *delimiterlocation = 0;
>
> /* return location after what we just changed to
> null, which is the rest of the string */
> return (delimiterlocation + 1);
> }
> }
>
> /* we hit the end of the string without finding
> the delimiter, so return NULL to indicate that */
> return NULL;
> }
>
>Once you have a function like that, you can just call it in a loop
>until it returns NULL. Of course, you have to make sure that the
>string you pass it can be written to. (You can't legally pass it
>a constant string. For instance, you couldn't legally do
>replacedelimiter ("12.34", '.') beceause the inline "12.34" is
>a constant. But, the compiler should warn you about this in
>many cases if you make that mistake.)
>
>Anyway, a loop to show all the pieces of the string might look
>like this:
>
> Char *piece;
> Char *remainder;
> Char tobesplit[] = "1-800-555-1212";
>
> remainder = tobesplit;
> do
> {
> piece = remainder;
> remainder = replacedelimiter (remainder, '-');
> FrmCustomAlert (MyAlert, piece, "", "");
> } while (remainder != NULL);
>
>The above code is a little confusing because piece and remainder are
>set to the same pointer value (same location in memory), and then
>replacedelimiter() changes that memory, and only after it has changed
>is it true that piece points to a piece, because although it's always
>pointing to the same place, the null terminator hasn't been written
>into memory to break it apart yet.
>
> - Logan
Thanks for this very helpful code!!!
That is just what I needed!
Oliver
--
For information on using the Palm Developer Forums, or to unsubscribe, please
see http://www.palmos.com/dev/support/forums/