Author Topic: string manipulation question  (Read 4037 times)

Offline Sixth Sense

  • Mc. Print
  • *
  • Posts: 47
    • View Profile
    • Sixth Sense
string manipulation question
« on: 2010-Nov-07 »
x=mid$(source$,start,length) returns a part of the source$.

is there an easy way to replace 1 letter in a string. (replace$ replaces all occurrences)

ie. the equivalent of:         mid$(dest$,start,1)="x"





Okay, It’s been more than ten years now so I confess............I let the dogs out!

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4143
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: string manipulation question
« Reply #1 on: 2010-Nov-07 »
Something like REPLACE$, perhaps ;) :P
I came. I saw. I played.

Offline Sixth Sense

  • Mc. Print
  • *
  • Posts: 47
    • View Profile
    • Sixth Sense
Re: string manipulation question
« Reply #2 on: 2010-Nov-07 »
thanks Ian but I tried replace$ and as I mentioned above it replaces all instances of the search$

At the moment I'm splitting using left$ and right$ adding the replacement letter between the splits and joining them back together again. (very messy)
Okay, It’s been more than ten years now so I confess............I let the dogs out!

MrTAToad

  • Guest
Re: string manipulation question
« Reply #3 on: 2010-Nov-07 »
Yes, something like that would be nice.

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4143
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: string manipulation question
« Reply #4 on: 2010-Nov-07 »
Sorry, I misread the question (I've only just got up after a nightshift and have had flu all week, so head filled with cotton wool!).
I came. I saw. I played.

Offline ketil

  • Mr. Polyvector
  • ***
  • Posts: 117
    • View Profile
Re: string manipulation question
« Reply #5 on: 2010-Nov-07 »
PEEK and POKE :)

And then we would need GETPTR/READBUFFER (or something like that) to get an pointer to the databuffer of certain objects.
It could be used with the mentioned PEEK (as READ) an POKE.
Just like basic back in the days on the microcomputers :)
"Sugar makes the world go 'round. Caffeine makes it spin faster."

MrTAToad

  • Guest
Re: string manipulation question
« Reply #6 on: 2010-Nov-07 »
Peek & Poke has already been done!

Offline Synthetic

  • Mr. Drawsprite
  • **
  • Posts: 61
    • View Profile
    • ClanBam
Re: string manipulation question
« Reply #7 on: 2010-Nov-07 »
Here's a simple function to do that. You can specify any position in the string and the amount of characters to change.

string$ is the input string you wish to modify
alter$ is the text you wish to append/alter in string$
start_pos is the starting position in string$ where the change will take
end_pos is the ending position in string$ where the change will take place

Code: GLBasic [Select]
FUNCTION string_replace$: string$,alter$,start_pos,end_pos
        LOCAL out$
                out$ = MID$(string$,0,start_pos) + alter$ + MID$(string$,end_pos,-1)
        RETURN out$
ENDFUNCTION

Sample Usage:
Code: GLBasic [Select]
a$ = "A dog that's big"
b$ = "cat"
print string_replace$(a$,b$,2,5),0,0

Will produce the following output:
A cat that's big


"Impossible is a word people use to make themselves feel better when they quit."

My AMXMODX plugins for Day of Defeat 1.3 can be found here.

MrTAToad

  • Guest
Re: string manipulation question
« Reply #8 on: 2010-Nov-08 »
Why not do away with the local variable, and just do :

Code: GLBasic [Select]
FUNCTION string_replace$: string$,alter$,start_pos,end_pos
        RETURN MID$(string$,0,start_pos) + alter$ + MID$(string$,end_pos,-1)
ENDFUNCTION

Offline Synthetic

  • Mr. Drawsprite
  • **
  • Posts: 61
    • View Profile
    • ClanBam
Re: string manipulation question
« Reply #9 on: 2010-Nov-08 »
Quite right. I think I've been writing too many functions these past 7 days where I needed to manipulate the return more.
"Impossible is a word people use to make themselves feel better when they quit."

My AMXMODX plugins for Day of Defeat 1.3 can be found here.

Offline ketil

  • Mr. Polyvector
  • ***
  • Posts: 117
    • View Profile
Re: string manipulation question
« Reply #10 on: 2010-Nov-08 »
Peek & Poke has already been done!

No.
It has not. At least not as default commands in GLB.
Maybe in an external library ?
If so, is there anybody that could provide a link ?
"Sugar makes the world go 'round. Caffeine makes it spin faster."

Offline ketil

  • Mr. Polyvector
  • ***
  • Posts: 117
    • View Profile
Re: string manipulation question
« Reply #11 on: 2010-Nov-08 »
This is fast ...

Code: GLBasic [Select]
        LOCAL s$ = "- OK -"
       
        PRINT s$, 10, 10

        POKE(s$, 1, "P")       
        POKE(s$, 4, "E")       

        PRINT s$, 10, 30

        SHOWSCREEN
        MOUSEWAIT

        END
       

       
        FUNCTION POKE: BYREF src$, pos%, ch$
        // return FALSE if pos% is out of bounds
        LOCAL rc% = TRUE
        INLINE
                unsigned int n = src_Str.len();
                if(n >= pos)    {
                        // Do getbuffer() not allocate if already initialized(and allocated) ? if i set length to 0 or 1, it still returns textbuffer and is writable. anybody ?
                        char * tmp_ = src_Str.getbuffer(n);
                        tmp_[pos]= ch_Str[0];
                        // Unless getbuffer() just return an pointer when DGStr is initialized, we have to use this (i don't know if that's the case)
                        src_Str.releasebuffer();       

                } else {
                        rc=0;
                }
                       
        ENDINLINE
        ENDFUNCTION

 

"Sugar makes the world go 'round. Caffeine makes it spin faster."

MrTAToad

  • Guest
Re: string manipulation question
« Reply #12 on: 2010-Nov-08 »
« Last Edit: 2010-Nov-08 by MrTAToad »

Offline ketil

  • Mr. Polyvector
  • ***
  • Posts: 117
    • View Profile
"Sugar makes the world go 'round. Caffeine makes it spin faster."

Offline Sixth Sense

  • Mc. Print
  • *
  • Posts: 47
    • View Profile
    • Sixth Sense
Re: string manipulation question
« Reply #14 on: 2010-Nov-08 »
I should  have noticed that a value of <0 for the length param in mid$ would read string to end. (that's what happens when you get old!)

 :nw: Thanks to everyone who pitched in, problem solved. :nw:

Syntheetic's function suits the job in hand and Ketil and MrTAToad's information on peek & poke has given me other ideas for the future.

 :good:
Okay, It’s been more than ten years now so I confess............I let the dogs out!