Monkey equivalent to Blitz3D Mid$() command.

Monkey Forums/Monkey Code/Monkey equivalent to Blitz3D Mid$() command.

Steve Ancell(Posted 2013) [#1]
I couldn't seem to find an equivalent to the Blitz3D Mid$() command, unless I've missed it somewhere; so I made my own.

It has only been tested on the HTML5 target so far, but seems to be working fine in that case.

[edit]I have now tested it in GLFW too, seems good so far.[/edit]


Function Mid:String(inString:String, startPos:Int, charCount:Int)
	Local stringArray:String[] = inString.Split("")
	Local outAsc:String = ""
	Local i:int
	Local startPosFlag:Bool
	Local charCountFlag:Bool
	
	If startPos > 0 And startPos <= inString.Length()
		startPosFlag = True
	Else
		startPosFlag = False
	EndIf
	
	If charCount > 0 And charCount <= inString.Length() - (startPos - 1)
		charCountFlag = True
	Else
		charCountFlag = False
	EndIf
	
	If startPosFlag = True And charCountFlag = True
		For i = startPos To startPos + (charCount - 1)
			outAsc += stringArray[i - 1]
		Next
	EndIf
	
	If startPosFlag = False
		outAsc = "Error! startPos out of range"
	EndIf
	
	If charCountFlag = False
		outAsc = "Error! charCount out of range"
	EndIf
	
	If startPosFlag = False And charCountFlag = False
		outAsc = "Error! startPos and charCount out of range"
	EndIf
	
	Return outAsc
End



Goodlookinguy(Posted 2013) [#2]
That's a bit long-winded for something simple...
Function Mid:String( str:String, pos:Int, size:Int = -1 )
	'if aiming to emulate a starting index of 1, uncomment the next line
	'pos -= 1
	If size = -1 Then Return str[pos..]
	Return str[pos..pos + size]
End



Steve Ancell(Posted 2013) [#3]
Ahh!, but the more long winded, the more I get to experiment. ;)


Steve Ancell(Posted 2013) [#4]
Here's a modified version, based on the code snippet that Goodlookinguy provided above but with error trapping, thanks for pointing out that handy feature. ;)

Function Mid:String(inString:String, startPos:Int, charCount:Int)
	Local outAsc:String
	Local i:int
	Local startPosFlag:Bool
	Local charCountFlag:Bool
	
	If startPos > 0 And startPos <= inString.Length()
		startPosFlag = True
	Else
		startPosFlag = False
	EndIf
	
	If charCount > 0 And charCount <= inString.Length() - (startPos - 1)
		charCountFlag = True
	Else
		charCountFlag = False
	EndIf
	
	If startPosFlag = True And charCountFlag = True
		outAsc = inString[startPos - 1 .. startPos + charCount - 1]
	EndIf
	
	If startPosFlag = False
		outAsc = "Error! startPos out of range"
	EndIf
	
	If charCountFlag = False
		outAsc = "Error! charCount out of range"
	EndIf
	
	If startPosFlag = False And charCountFlag = False
		outAsc = "Error! startPos and charCount out of range"
	EndIf
	
	Return outAsc
End



Goodlookinguy(Posted 2013) [#5]
You're making it too hard on yourself. Here, I've cleaned up your code a bit. Also, people don't like code that doesn't nicely catch problems. This doesn't nicely catch problems. It changes the text they input into other text completely if there's a problem. Just a thought.




Steve Ancell(Posted 2013) [#6]
I've been out of the programming scene for quite a few months, due to losing my dad a little while back, so I do tend to go the long way around with my coding at the moment.

Thank you for sorting out this code snippet, you've been a great help. ;)