something strange is happening, $replace not working
#1
I could be having a brain misfire, but I could swear the $replace function worked in QB64, why am I getting an illegal string-number conversion on line 9? 

Code: (Select All)
Dim sOld$
Dim sOldWord$
Dim sNewWord$
Dim sNew$

sOld$ = "This is my sentence to change my words."
sOldWord$ = "my"
sNewWord$ = "your"
sNew$ = Replace$(sOld$, sOldWord$, sNewWord$)

Print "Original: " + Chr$(34) + sOld$ + Chr$(34)
Print "Replace : " + Chr$(34) + sOldWord$ + Chr$(34)
Print "With    : " + Chr$(34) + sNewWord$ + Chr$(34)
Print "New     : " + Chr$(34) + sNew$ + Chr$(34)
Reply
#2
Somebody here gave this function (sorry who was it ???)

Function strReplace$ (s$, replace$, new$) 'case sensitive
strReplace$ = s$
p = InStr(s$, replace$)
While p
strReplace$ = Mid$(strReplace$, 1, p - 1) + new$ + Mid$(strReplace$, p + Len(replace$))
p = InStr(p + Len(new$), strReplace$, replace$)
Wend
End Function
Why not yes ?
Reply
#3
As you don't show us the actual replace function, I can only make the best guess, that you misspelled the function name in the call in line 9. Hence, the compiler doesn't recognize Replace$ as a function and going to think its an un-DIMed array. But as you give strings in the paranthesis, where an array would expect index numbers, you get the "illegal string-number conversion" error.

It's one of those errors, where the error doesn't really has a connection to the real mistake, but is just caused by a "symtom" of it.

Check the function name, that's most probably the cause of it. (StrReplace$() maybe??)
Reply
#4
Have a glance also here
https://qb64forum.alephc.xyz/index.php?t...#msg112620
https://qb64forum.alephc.xyz/index.php?t...#msg121291
Why not yes ?
Reply
#5
Oh, yes, StrReplace$ is part of the QB64 source (source\utilities\strings.bas). But you must of course $INCLUDE that file in your program to use it.
Reply
#6
(07-25-2022, 03:15 PM)RhoSigma Wrote: Oh, yes, StrReplace$ is part of the QB64 source (source\utilities\strings.bas). But you must of course $INCLUDE that file in your program to use it.

Thanks for your reply. 
So it ISN'T a built in function. 
I've been using it for so long, I can't remember - my coffee has not kicked in yet, and for some reason searching for the function didn't yield any results so I figured it must be a built in command. When back at my PC later I'll check to see if my notepad++ search settings were maybe stuck on "case sensitive" or something. I definitely got the function from "strings.bas". 
Thanks again for the sanity check.
Reply




Users browsing this thread: 4 Guest(s)