Strings

BlitzMax Forums/BlitzMax Beginners Area/Strings

I haven't really messed with strings..

I'm looking for some basics on this.. Trying to figure out how to make a notepad like interface using just strings.. I'm using the MaxGUI, I want to add in my text to a txt-field then have it show on a canvas with strings.

What I'm trying to do is convert some Text into graphics like "(W)" Makes a Image from that string.

Can you let me know.

The code below lets you enter your text which is than shown on a canvas. Grab it with grabimage if you like to store it as image...

Local Window1:TGadget = CreateWindow:TGadget("Window1",260,91,356,248,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE |WINDOW_STATUS |WINDOW_CLIENTCOORDS )
	Local BorderPanel1:TGadget = CreatePanel:TGadget(7,97,341,145,Window1:TGadget,PANEL_BORDER,"BorderPanel1")
		SetGadgetLayout( BorderPanel1:TGadget,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED )
		Local Canvas1:TGadget = CreateCanvas:TGadget(0,0,337,141,BorderPanel1:TGadget,Null)
			SetGadgetLayout( Canvas1:TGadget,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED )
	Local Label1:TGadget = CreateLabel:TGadget("Your text:",7,15,80,14,Window1:TGadget,Null)
		SetGadgetLayout( Label1:TGadget,EDGE_ALIGNED,EDGE_CENTERED,EDGE_ALIGNED,EDGE_CENTERED )
	Local Label2:TGadget = CreateLabel:TGadget("Text on canvas:",7,80,163,14,Window1:TGadget,Null)
		SetGadgetLayout( Label2:TGadget,EDGE_ALIGNED,EDGE_CENTERED,EDGE_ALIGNED,EDGE_CENTERED )
	Local TextField1:TGadget = CreateTextField:TGadget(7,35,344,18,Window1:TGadget,Null)
		SetGadgetText( TextField1:TGadget,"")
		SetGadgetLayout( TextField1:TGadget,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_CENTERED )

Repeat
	WaitEvent()
	Select EventID()
		Case EVENT_WINDOWCLOSE
			Select EventSource()
				Case Window1	Window1_WC( Window1:TGadget )
			End Select

		Case EVENT_GADGETACTION
			Select EventSource()
				Case TextField1	TextField1_GA( TextField1:TGadget )
			End Select

		Case EVENT_GADGETPAINT
			Select EventSource()
				Case Canvas1	Canvas1_GP( Canvas1:TGadget )
			End Select

	End Select
Forever

Function Window1_WC( Window:TGadget )
	DebugLog "Window Window1 wants to be closed"
'	HideGadget( Window:TGadget )

	End
End Function

Function TextField1_GA( TextField:TGadget )
	DebugLog "TextField TextField1 was modified"
	DebugLog "Text = "+ TextFieldText$( TextField:TGadget )
	Cls
	DrawText(TextFieldText(TextField),10,10)
	Flip
End Function

Function Canvas1_GP( Canvas:TGadget )
	DebugLog "Canvas Canvas1 needs to be redrawn"
	SetGraphics CanvasGraphics ( Canvas )
	SetViewport 0,0,GadgetWidth( Canvas ),GadgetHeight( Canvas )
	
	'Init Canvas
	SetColor( 208,216,61 )
	SetClsColor( 118,131,184 )
	Cls
	DrawText( "Type some text in the textfield!" ,1,1 )
	Flip
	'End of init

End Function



But what if I want to change a set of text like :SAD: into a image like they do on the forums..

I think he means like MSN Messenger etc changes :) into a smiley etc.

Replace the function below and type somewhere in your text in the texfield :) or :( or ;) to see how it triggers. Of course if you want some graphics, replace the drawtext command with a drawimage and your own image.

Function TextField1_GA( TextField:TGadget )
	DebugLog "TextField TextField1 was modified"
	DebugLog "Text = "+ TextFieldText$( TextField:TGadget )
	Cls
	DrawText(TextFieldText(TextField),10,10)
	If TextFieldText(TextField).Contains(":)") Then DrawText("Smiley :)",10,50)
	If TextFieldText(TextField).Contains(":(") Then DrawText("Smiley :(",10,80)
	If TextFieldText(TextField).Contains(";)") Then DrawText("Smiley ;)",10,100)
	Flip
End Function


Some useful string functions...
Find:Int( subString:String,startIndex=0 ) Finds first occurance of a sub string. Returns -1 if subString not found.
FindLast:Int( subString:String,startIndex=0 ) Finds last occurance of a sub string. Returns -1 if subString not found.
Trim:String() Removes leading and trailing non-printable characters from a string.
Replace:String( subString:String,withString:String ) Replaces all occurances of subString with withString.
StartsWith:Int( subString:String ) Returns true if a string starts with subString.
EndsWith:Int( subString:String ) Returns true if a string ends with subString.
Contains:Int( subString:String ) Returns true if a string contains subString.
ToLower:String() Converts a string to lowercase.
ToUpper:String() Converts a string to uppercase.
ToInt:Int() Converts a string to an integer.

Okay so, what if I wanted the image with in the text itself.. how would I find its position I tried len()

If you want to find a position just use find!
SuperStrict
Local A:String
A$="blabla:) and more blabla;)"
Print "Position="+A.find(":)")
Print "Position="+A.find(";)")

If you want to do a Messenger like thing, i would probably store all :) ;) and such as tokens. Also a Tmap may ok, to retrieve the images handles for a certain string “:-)”.
Maybe this all too advanced and you like to stick to the old basic commands, you can do so as well:
Use Instr( str$,sub$,start=1 ) to find your substring and cut it in pieces with Left$, Right$ and Mid$, check out the docs on this.