mid$ or how replace a byte in a long string...

Previous topic - Next topic

Marmor

how to replace a char in a string ?
problem :

ok$="mammmmmmaaaammmm etc etc" <- really long string
mid$(ok$,2,1)="-"  give a syntax error
cant use replace command here .
cut a string into pieces to replace one byte in a string is imho  :puke:
because the speed .
maybe the replace command needs a pointer or something.
any idea ?
thx


CW

You have the right idea, but you are using mid$() incorrectly. The proper usage  is like this, Ch$=mid$(Ok$,2,1)
which will return the third character in the Ok$ string. (The second char, actually, counting from zero.)
But if you are looking to replace one character or one string with another, that particular command can't do the job on its own.
I wrote a couple quick programs, one of which may be what you are after.
The first one simply replaces one char with another.

Code (glbasic) Select

String$ = "mammmmmmaaaammmm" // Let's replace every "a" with "-"

LookFor$ = "a";ReplaceWith$="-"
NewString$ = ""

FOR counter = 0 TO LEN(String$)
hold$ = MID$(String$,counter,1)
IF hold$ = LookFor$
NewString$ = NewString$ + ReplaceWith$
ELSE
NewString$ = NewString$ + hold$
ENDIF
NEXT

PRINT String$,10,10
PRINT NewString$,10,30
SHOWSCREEN;KEYWAIT;END



Or is the next function more of what you are looking for? It replaces entire strings of characters (full words).

I put all of the string-handling on a single line for compactness, to minimize variables needed and to optimize run time.
(This practice produces hard to read, debug or modify code and is generally frowned upon.)
If you would like the steps broken down so you can study them, just let me know and I be happy to do it for you.

Happy Coding!
-CW

Code (glbasic) Select

// (Excuse the mousefont. I am trying to keep things simple. Please add a font for readability if you wish it.)

String$ = "I love my dog. My dog is the best! But when I call my dog, my dog ignores me."
//Let's replace every 'dog' with a 'cat'. (Because cats rule, and dogs drool!) =^..^=

PRINT String$,1,1
ReplaceString("dog",String$,"cat") // If you hover over the function, you will see the syntax.
PRINT String$,1,20

SHOWSCREEN
KEYWAIT
END
///////////////////////////////////////////////    REPLACESTRING     ///////////////////////////////////////////////////////////
//                                                    Overview:
// This function finds all instances of Find$ embedded in a larger string (In_String$), and replaces them all with Replace_With$
// Process: The active line looks for the first instance of Find$, takes the text to the left of it, adds the replacement string,
// and then adds all of the text found to the right of it. (Find$ text is omitted from the new string.)
// The newly reconstructed string is then assigned back to the original String$ and the process repeats until all instances of
// Find$ have been replaced. Note that this function is case-sensitive. Use UCASE$() or LCASE$() if you want both caps and
// lower-case versions of the target text changed.
//
// Note that a function can only return a single value, so to deal with an entire string you either have to use a GLOBAL variable,
// or, better yet, do a BYREF, which creates a pointer to the ORIGINAL string. The original string gets changed.
// "In_String$" is the name of the pointer. It is not a variable, but merely points at the original variable in memory that you
//  sent to the function. You can think of the pointer as a second name for the same variable. That's not quite right, but it's close
// close enough. In reality, the variable has a unique name, the pointer has a unique name, and the pointer points to the variable.
// However you think about it, don't confuse pointers with variables. They are very different tools.
//
FUNCTION ReplaceString:Find$,BYREF In_String$,Replace_With$

WHILE INSTR(In_String$,Find$,0) > -1 // Note: a -1 means the INSTR() command didn't find any instances of Find$, so we should exit.

String$=LEFT$(In_String$,INSTR(In_String$,Find$,0))+Replace_With$+RIGHT$(In_String$,(LEN(In_String$)-LEN(Find$)-INSTR(In_String$,Find$,0)))

WEND

ENDFUNCTION

Marmor

thx for your cool code cw  :)

but  i will replace only one char in position x from source$
so i mean a for next loop or cut the string in 2 pieces, add my char and put the result together is .... :x
ill prefer a command like  replace$(source$,start,"xyz")
or  mid$(source$,start)="-" 



CW

Hi Marmor.

There isn't a command which will replace chars within a string, so you will have to write your own. The commands from my first example will help you do the job.

As for the name of the function, you mentioned liking Replace() or Mid$(). Other languages allow programers to repeat the names of functions, a practice called overloading (which can be quite handy), but GLbasic doesn't allow that, so far as I know. Mid$() is used by GLbasic, so you won't be able to use it. But you can certainly make a function called Replace(), which will do exactly what you want.

You are right about how you will have to cut and past to get the output you need. Sometimes there is no alternative but to roll up your sleeves, jump right in and use some brute force. I suspect that may be the case with my own problem which I posted to this board. If GLbasic doesn't have a command for a thing, you have no choice but to fall back on your own programing which, written in BASIC, can be very slow compared to C++ or other more native level languages. Anyway, I'm rambling.

Just yell if you need a hand.  :good:
-CW

CW

Well, SHUT MY MOUTH!

There is a Replace$() command! I'm sorry Marmor. I completely misunderstood what you were asking.
You have been very nice, but now you can just ignore me.

Here I've been replacing text the hard way all this time.  :S
Now if they can only perfect the DoWhatIMean() command, then I would be set.
LOL. Ah well. At least I learned something new today. 

Cheers!
-CW