string manipulation question

Previous topic - Next topic

Sixth Sense

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!

Ian Price

Something like REPLACE$, perhaps ;) :P
I came. I saw. I played.

Sixth Sense

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

Yes, something like that would be nice.

Ian Price

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.

ketil

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

Peek & Poke has already been done!

Synthetic

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

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

Synthetic

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.

ketil

Quote from: MrTAToad on 2010-Nov-07
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."

ketil

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

#12

ketil

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

Sixth Sense

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!