Mid(MyString$, Offset, Characters)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Mid(MyString$, Offset, Characters)

Post by RichardL »

Some other BASICs I have used allow you to select all characters to the right of a position in a string using the syntax:

RParts$ = mid(MyString$, Offset)

PB always requires the three paramaters nomally associated with Mid(), so I need to write:

RPart$ = Mid(MyString$, N, Len(MyString$)-N+1) ; Messy

or:

RParts$ = Mid(MyString$, N, BigNumber) ; Works but not 'clean'

where I am sure that BigNumber is longer than the balance of the string.

Please could PB support the two parameter version of the call?
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

Heard about Left() and Right()?
lol... :lol:
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

How does Left() or Right() help here?
Left() starts at the beginning of the string and Right() starts from the end.
Mid() is the only one that allows for a starting position within the string.

I agree it would be nice to have the string length in Mid() as an option.
If no length is specified then length to end of string is used.

Often I use:
Mid(String$,pos,Len(String$))

Since PB seems to stop when the end of the string is reached, and this guarantees your length will exceed what is remaining in your string. ;)
Image Image
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

Yes, of course! :D

(I have been programming with various BASICS for about 25 years.)

If I need all of a string to the right of a particular position, and the string does not have a fixed length Right() does not help.

Mid(String$, Start, Length) gves me what I want, but I have to calculate the value of Length.

PB stores strings with NULL terminators so the additional logic in MID() to (a) Detect no third parameter and set a flag then (2) Maka string of everything from 'Start' to the NULL into the returned value probably takes about as much work as writing this note!
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

Hi Paul,

Yes, you do much the same as I do. Using Len(String) is safe in PB, but I think its 'untidy'. :)

Richard
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

RichardL wrote:(I have been programming with various BASICS for about 25 years.)
Rule 1: Don't assume PureBasic is a BASIC dialect.
Image Image
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

I think its a good idea. I have needed this a few times only, however it would definently be more clean this way.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

I'm using this:

Code: Select all

Macro Mids(A,n)
   Mid(A,n,Len(A))
EndMacro 
  
b$="abcdefgh"
Debug Mids(b$,2)

Repeat 
Until WaitWindowEvent()=#PB_Event_CloseWindow  
 
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Paul wrote:How does Left() or Right() help here?

Code: Select all

Macro MidInf(String, StartPos)
  Right(String, Len(String)+1-StartPos)
EndMacro
Edit: I'm too slow.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

Trond:
Your macro is clever but slower.

Code: Select all

Macro MidInf(String, StartPos)
  Right(String, Len(String)+1-StartPos)
EndMacro
 
Macro Mids(A,n)
   Mid(A,n,Len(A))
EndMacro 
  
b$="abcdefgh"

ti=GetTickCount_()
For i=0 To 100000
A$= Mids(b$,2)
Next
Debug GetTickCount_()-ti

ti=GetTickCount_()
For i=0 To 100000
  A$= MidInf(b$,2)
Next
Debug GetTickCount_()-ti

Repeat 
Until WaitWindowEvent()=#PB_Event_CloseWindow  
 
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Faster:

Code: Select all

Macro Mids(A,n)
  PeekS(@A+n-1)
EndMacro
:twisted:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

NEVER EVER NEVER EVER NEVER EVER enable the debugger when you're doing speed tests. Turn off the debugger and mine is faster.

But I didn't realize this was a speed competition. This beats everything so far:

Code: Select all

Macro MidInf2(String, StartingPos)
   PeekS(@String + SizeOf(Character)*(StartingPos-1))
EndMacro
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

even faster (macro is redundant)

Peeks(@string$+ n)
Last edited by netmaestro on Thu May 04, 2006 8:05 pm, edited 1 time in total.
BERESHEIT
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Trond wrote:NEVER EVER NEVER EVER NEVER EVER enable the debugger when you're doing speed tests. Turn off the debugger and mine is faster.

But I didn't realize this was a speed competition. This beats everything so far:

Code: Select all

Macro MidInf2(String, StartingPos)
   PeekS(@String + SizeOf(Character)*(StartingPos-1))
EndMacro
:lol: speed war strikes again
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

netmaestro wrote:even faster (macro is redundant)

Peeks(@string$, n)
But far from the same functionality :wink:

EVEN faster:

Code: Select all

Z + 1 - 3
Post Reply