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"
Something like REPLACE$, perhaps ;) :P
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)
Yes, something like that would be nice.
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!).
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 :)
Peek & Poke has already been done!
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
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:
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
Why not do away with the local variable, and just do :
FUNCTION string_replace$: string$,alter$,start_pos,end_pos
RETURN MID$(string$,0,start_pos) + alter$ + MID$(string$,end_pos,-1)
ENDFUNCTION
Quite right. I think I've been writing too many functions these past 7 days where I needed to manipulate the return more.
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 ?
This is fast ...
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
Peek And Poke can be found here : http://www.glbasic.com/forum/index.php?topic=2853.msg20932#msg20932
Quote from: MrTAToad on 2010-Nov-08
Peek And Poke can be found here : http://www.glbasic.com/forum/index.php?topic=2853.msg20932#msg20932
Thanks, MrTAToad :good:
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: