Equivalent of VB Mid Statement (not Function)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
ebs
Enthusiast
Enthusiast
Posts: 558
Joined: Fri Apr 25, 2003 11:08 pm

Equivalent of VB Mid Statement (not Function)

Post 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...
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post 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 :)
DominiqueB
Enthusiast
Enthusiast
Posts: 103
Joined: Fri Apr 25, 2003 4:00 pm
Location: France

Hello

Post by DominiqueB »

Did you try the Replacestring() function, it does what you want i think ?
Dominique

Windows 10 64bits. Pure basic 32bits
ebs
Enthusiast
Enthusiast
Posts: 558
Joined: Fri Apr 25, 2003 11:08 pm

Post 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
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post 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)

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
ebs
Enthusiast
Enthusiast
Posts: 558
Joined: Fri Apr 25, 2003 11:08 pm

Post 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
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Equivalent of VB Mid Statement (not Function)

Post 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.
ARGENTINA WORLD CHAMPION
Post Reply