GLBasic forum

Main forum => GLBasic - en => Topic started by: Sixth Sense on 2010-Nov-07

Title: string manipulation question
Post by: Sixth Sense 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"





Title: Re: string manipulation question
Post by: Ian Price on 2010-Nov-07
Something like REPLACE$, perhaps ;) :P
Title: Re: string manipulation question
Post by: Sixth Sense 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)
Title: Re: string manipulation question
Post by: MrTAToad on 2010-Nov-07
Yes, something like that would be nice.
Title: Re: string manipulation question
Post by: Ian Price 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!).
Title: Re: string manipulation question
Post by: ketil 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 :)
Title: Re: string manipulation question
Post by: MrTAToad on 2010-Nov-07
Peek & Poke has already been done!
Title: Re: string manipulation question
Post by: Synthetic 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


Title: Re: string manipulation question
Post by: MrTAToad 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
Title: Re: string manipulation question
Post by: Synthetic 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.
Title: Re: string manipulation question
Post by: ketil on 2010-Nov-08
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 ?
Title: Re: string manipulation question
Post by: ketil 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



Title: Re: string manipulation question
Post by: MrTAToad on 2010-Nov-08
Peek And Poke can be found here  : http://www.glbasic.com/forum/index.php?topic=2853.msg20932#msg20932
Title: Re: string manipulation question
Post by: ketil on 2010-Nov-08
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:
Title: Re: string manipulation question
Post by: Sixth Sense 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: