Page 1 of 1

Equivalent of VB Mid Statement (not Function)

Posted: Tue Jun 03, 2003 10:32 pm
by ebs
Please add a MidString() statement (not function) which would replace specified characters in a string with another string. The syntax could be something like

Code: Select all

MidString(String.s, Start.l, Length.l, NewString.s)
or

Code: Select all

MidString(String.s, Start.l, Length.l) = NewString
So this code

Code: Select all

OrigString.s = "1234567890"
NewString.s = "HELLO"
MidString(OrigString, 4, NewString, 4)
Debug MidString
would result in "123HELL890".

I know this can be done with PokeS(), but that puts the '0' byte at the end of the poked string, which causes the original string to end there. Right now, I use my own function which saves and restores the byte at the end of the poked string, but a native function would be cleaner.

Maybe just a slightly different PokeS() function which doesn't poke the '0' byte...

Posted: Tue Jun 03, 2003 10:48 pm
by Berikco
String$ = ReplaceString(String$, StringToFind$, StringToReplace$ [, Mode [, StartPosition]])
Description

Try to find any occurrences of 'StringToFind$' in the given 'String$' and replace them with 'StringToReplace$'. An optional 'StartPosition' parameter can be specified to tell at which position the replace should start. First 'StartPosition' index is 1.

'Mode' can be a combination of the following values:
1: Case insensitive search (A=a). By default the search is case sensitive.
2: In place replacing. This means that the string is replaced directly in the memory. The 'StringToFind$' and
'StringToReplace$' parameter must have the same length ! This is a dangerous option, for advanced users only.
The advantage is the very high speed of the replacement.

Edit: Ha, not exacly what you're looking for :)

Hello

Posted: Tue Jun 03, 2003 10:55 pm
by DominiqueB
Did you try the Replacestring() function, it does what you want i think ?

Posted: Tue Jun 03, 2003 11:05 pm
by ebs
Dominique,

Thanks for the suggestion, but ReplaceString() searches for a specified string and then replaces all occurrences of that string with another string. I just want to replace specified characters.

Regards,
Eric

Posted: Tue Jun 03, 2003 11:31 pm
by LarsG
Something like this maybe?!?
It's not optimized or anything, but you can play with it and it's
easy to understand how it works... :)

Code: Select all

OpenConsole()

string1.s = "1234567890"
string2.s = "HELLO"

InitKeyboard()

Procedure.s Insertstring(string1.s, offset.w, string2.s, lenght.w, overwrite)
  ; inserts or owerwrites string 2 with string 1
  temp1.s = Left(string1,offset)
  temp2.s = Right(string1, Len(string1)-Len(temp1))
  If overwrite = 1
    ret.s = temp1 + Mid(string2,0,lenght) + Right(temp2,Len(temp2)-lenght)
  Else
    ret.s = temp1 + Left(string2,lenght) + Mid(string1,offset+1,Len(string1))
  EndIf
  
  ProcedureReturn ret.s
EndProcedure



Print(insertstring(string1,3,string2,4,1))

Repeat
  ExamineKeyboard()
Until KeyboardPushed(1)

Posted: Wed Jun 04, 2003 1:33 am
by ebs
Here's what I'm doing now, using PokeS(). It works, and seems pretty efficient. I just think a native PB Mid() function would be cleaner and faster.

Code: Select all

; emulates Mid() *statement* in VB
; replace characters from Start with NewString for Length characters
Procedure MidString(*String, Start.l, Length.l, NewString.s)
  ; calculate start position
  ; remember to adjust since string positions start at 1
  StartPos.l = *String + Start - 1
  ; save character at end of poked string 
  SaveChar.b = PeekB(StartPos + Length)
  ; poke new string
  PokeS(StartPos, NewString, Length)
  ; replace saved character (on top of '0')
  PokeB(StartPos + Length, SaveChar)
EndProcedure
Called like this:

Code: Select all

Record.s = "1234567890"
Debug "Before: " + Record
MidString(@Record, 4, 4, "HELLO")
Debug "After: " + Record
Any suggestions or improvements are welcome.

Eric

Re: Equivalent of VB Mid Statement (not Function)

Posted: Wed Jun 04, 2003 3:04 am
by ricardo
But its not MidString AFAK, its Replace. (in VB)

ReplaceString in PB its uncomplete since it replace ALL ocurrences and this is great for some uses, but sometimes you want to replace just SOME occurences, then you need to have not only a start parameter but a long parameter too.

Could be very usefull to add it, so many time i need it.