extract & insert a character from a string

BlitzMax Forums/BlitzMax Programming/extract & insert a character from a string

Gary Leeds(Posted 2010) [#1]
I am looking to write a text scroller routine that is a 16 character display and am stuck on extracting and inserting a single character from a string

I am using a string called alphatext$ to store the string to be displayed and another called textdisplay$ for the actual text to be displayed

I want the choice of static or scrolling displays so if the first character of alphastring$ is * then it denotes a scrolling message, anything else is a static message

currently my code is

	If  alphatext[0] = "*" And scroll = 0
		scroll = 1
		pos = 1
		scrolldelay = MilliSecs()+10
	EndIf
	
	If alphatext[0] = "*" And scroll =1 And scrolldelay < MilliSecs()
		chars=16
		textdisplay$="                "
		For a = pos To 1 Step -1
		textdisplay[chars-1]=alphatext[a]
		chars = chars - 1
		If chars = 0 Then a = 1
		scrolldelay = MilliSecs()+10
		Next
	EndIf


where it fails to complile is the textdisplay[chars-1]=alphatext[a] line. i get the error "expression must be a variable"

What is the correct (and hopefully easy way) to pull a single character out of any position in a string and insert that into another string at a specific position?

Thanks in advance for your help

Gary


GfK(Posted 2010) [#2]
Your code is treating textdisplay$ as if its an array of strings, rather than a single string.

To extract a single character from a string, use the Mid() function.

Not sure on the best way to insert a character back into a string... you could split the string in two using left() and right(), then concatenate the results with the inserted character between... or maybe use slices.

I'll get back to you if nobody who already has the answer beats me to it.

[edit]
Local s:String = "Hello_World!"

Print getCharFromString(s,5)
Print insertCharIntoString(s,"X",5)

Function insertCharIntoString:String(txt:String,char:String,pos:Int)
	Local l:String = txt[..pos-1]
	Local r:String = txt[pos-1..]
	Return l + char + r
End Function

Function getCharFromString:String(txt:String,pos:Int)
	Return Mid(txt,pos,1)
End Function



beanage(Posted 2010) [#3]
Extraction can be done via array indexing, which is hopefully a lot faster:
Function getCharacterFromString:String(txt:String,pos:Int)
	Return Chr(txt[pos])
End Function

-- edit --
Also, the indizes of the <insertCharIntoString> function above are mixed up, correctly the function has to be:
Function insertCharIntoString:String(txt:String,char:String,pos:Int)
	Local l:String = txt[..pos]
	Local r:String = txt[pos+1..]
	Return l + char + r
End Function


Note, that pos must be zero-based (first character has position zero).


GfK(Posted 2010) [#4]
Also, the indizes of the <insertCharIntoString> function above are mixed
No they aren't. I tested it.


ziggy(Posted 2010) [#5]
Indexing is base zero, while the Mid function is base 1, so the Gfk example is correct.


beanage(Posted 2010) [#6]
@Gfk: I guess we are just working with different bases, but iirc something wasnt quite working when i tested your functions yesterday...