TGC Codebase Backup



Find out if String is a Float or Integer by Jack

30th Oct 2016 2:06
Summary

This function can Return the type of a String and find out if the String is a Float or an Integer.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
rem Project: Find_String_type - Alex Schmidt, online-arts.de
rem Created: 2016-03-15

// set window properties
SetWindowTitle( "Find_String_type" )
SetWindowSize( 1280, 720, 0 )

// set display properties
SetVirtualResolution( 1280, 720 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetPrintSize(20)






instr$="SomeRandomStringToShow"
type$=FindStringType(instr$)

instr2$="23402342.2342384"
type2$=FindStringType(instr2$)

instr3$="31238"
type3$=FindStringType(instr3$)

do
print("Find String Type : Online-arts.de : Alex Schmidt")
print("This function can find out if the string is an int,float or string.")
print("===========")
	print(instr$)
	print("="+type$)
print("===========")
	print(instr2$)
	print("="+type2$) 
print("===========")
	print(instr3$)
	print("="+type3$)
    
    Sync()
loop

// this function will find the type of the string. It can be an integer, a float or still remain a string
function FindStringType(intext as string)

out$ as string
point as integer
notnum as integer

// Insepct the String 
for char=1 to len(intext)
	
	if mid(intext,char,1)="." then point=point+1 // usally a point is inside a string or a float, but only a float has always one
	
		if asc(mid(intext,char,1))<48 or asc(mid(intext,char,1))>57 // check for all ascii values except numbers ...
			if asc(mid(intext,char,1))<>46 and asc(mid(intext,char,1))<>45 // ... "-" and "."
				notnum=notnum+1 
			endif
		endif
		
next char

// get results from expection
if notnum>0 or point>1 // if there are non-numerical chars
	out$="String"
	exitfunction out$
endif

if notnum=0 // then it could be a number
	
	if point=0 // if no point
		out$="Int"
		exitfunction out$
	endif
	
	if point=1 // so its float!
		out$="Float"
	exitfunction out$
	endif
endif




out$="Error in FindStringType"

endfunction out$