Code archives/Miscellaneous/Substring$(string1$,x,y)

This code has been declared by its author to be Public Domain code.

Download source code

Substring$(string1$,x,y) by Zach3D2005
Zach3D (Zach J. meier)
I wrote this because i use it alot and I think other people could use it(Please post your comments!)
;Substring Function(gets the string located at A,B of string)
Function Substring$(s1$,a,b)
length = b - a
J$ = Mid$(s1$,a,length + 1)
Return J$
End Function

trigstring$ = "Hello my name is Zach"
Print SubString$(trigstring$,3,8)
WaitKey()

;This prints "llo my"

Comments

Sonari Eclipsi Onimari2006
I hate to burst your bubble, but "INSTR" is a function that deals with this. But good thinking though... :)


Azathoth2006
Instr doesn't do this, Instr finds one string in another.


Perturbatio2006
He's right you know, Instr doesn't do this.

but I think mid$ might.


CS_TBL2006
mid$(string$,3,5)

The issue is whether you want "startposition - end position" or "startposition - amount", that function does the first, mid$ does the second..


Picklesworth2006
Exactly.
It's a simple function, but it makes working with strings appear much nicer.
Whenever I do stuff with strings, it ends up looking really confusing and messy because of things like Mid(string$,start,(end-start)+1)


CS_TBL2006
Only nitpick I could mention is the x,y .. typically you'd see those vars for anything 2d'ish, while this thing here reads from a 1d string. I'd avoid x,y and use x1,x2 or a,b or start,end etc.


Code Archives Forum