Hi everyone,
I wrote these two very simple functions so i could handle strings like i do in C#, i find it to be much simpler and easier to work with than the commands in DBPro when i want to load a file and work with it's contents.
IndexOf(String, Char) will return an integer that has the position of the char value in the string.
SubString(String1, startpos, endpos) This function will return a string whose start position and end position of String1 is defined with startpos and endpos. ( However, as it is now, when you seperate it by using the IndexOf at first to get a seperator, you have to deduct 1 from the substring's endpos you want to get returned that's in front of the seperator. Seems to work, but i find a bug i'll post a fix

)
Here's a code file with the functions and an example of how to use them.
a$ as string
a$ = "Hello;World!"
indexValue = IndexOf(a$,";")
Print a$
Print len(a$)
Print indexValue
Print " "
aSubString$ = SubString(a$, indexValue, len(a$)) `World
bSubString$ = SubString(a$, 0, indexValue - 1) `Hello
Print bSubString$
Print aSubString$
suspend for key
end
Function IndexOf(Input$ As String, Char$ As String)
local rightString$ As String
local characterString As String
local indexVal As Integer
For x = 1 to len(Input$)
leftString$ = Left$(Input$,x)
characterString$ = Right$(leftString$,1)
If characterString$ = Char$ then indexVal = x
Next x
EndFunction indexVal
Function SubString(subString$ As String, startIndex As Integer, length As Integer)
local leftSubString$ As String
local rightSubString$ As String
local rightPos As Integer
rightPos = len(subString$)-startIndex
leftSubString$ = right$(subString$, abs(rightPos))
rightSubString$ = left$(leftSubString$, length - startIndex )
EndFunction rightSubString$
..So yeah, i know these are very simple and not at all hard to code for yourself, but i thought i could share them since i already wrote them
-
Here's a modified IndexOf function, it get's the x y and z from a set of coordinates.
a$ as string
a$ = "Position: ;50 ;50 ;0 ;"
indexValue = IndexOfXYZ(a$,";")
`To get the position values from the string
xPos$ = SubString(a$, position(0),endPosition(0)) `x
yPos$ = SubString(a$, position(1),endPosition(1)) `y
zPos$ = SubString(a$, position(2),endPosition(2)) `z
Print a$
Print " "
Print xPos$
Print yPos$
Print zPos$
suspend for key
end
Function IndexOfXYZ(Input$ As String, Char$ As String)
`To get the position of the ; in the string
Global Dim position(3) as Integer
Global Dim endPosition(3) as Integer
`Find the position in the string just before each ; so we know the endvalues
`for the substring function
Global Dim xyz(3) as String
Global xyzCounter# as Integer
local rightString$ As String
local characterString As String
local indexVal As Integer
local doneOnce as Boolean = 0
local positionCounter# as Integer = 0
local endPositionCounter# as Integer = 0
local xyzCounter# as Integer = 0
For x = 1 to len(Input$)
leftString$ = Left$(Input$,x)
characterString$ = Right$(leftString$,1)
If characterString$ = Char$
position(positionCounter#) = x
inc positionCounter#
If doneOnce = 1
endPosition(endPositionCounter#) = x - 1
inc endPositionCounter#
Endif
doneOnce = 1
Endif
Next x
EndFunction indexVal
Function SubString(subString$ As String, startIndex As Integer, length As Integer)
local leftSubString$ As String
local rightSubString$ As String
local rightPos As Integer
rightPos = len(subString$)-startIndex
leftSubString$ = right$(subString$, abs(rightPos))
rightSubString$ = left$(leftSubString$, length - startIndex )
EndFunction rightSubString$