Getting a single char out of a string variable

Monkey Forums/Monkey Programming/Getting a single char out of a string variable

Fryman(Posted 2012) [#1]
Hi

I have looked through the language reference types section and I still dont quite understand it.

Im trying to take a single letter at a set position out of a string variable. I will not know what the string contains before hand

e.g. SomeString = "ABCD"

NewString = (Second Letter of) SomeString

The result of Print NewString should be "B"

Has something incredibly simple just gone right over my head?

Thanks in advance


DrT(Posted 2012) [#2]
Strict

Function Main:Int()
	Local someString:String = "ABCD"
	Local newString:String
	newString = someString[1..2]
	Print(newString)
	Return 0
End



DruggedBunny(Posted 2012) [#3]
Here's one way to do it, though it's not pretty!

Function Main ()

	Local test:String = "ABCD"
	
	Print String.FromChar (test [1])
	
End


Have a look at Language Reference -> Types -> The String type. [EDIT: Sorry, you did.]


DrT(Posted 2012) [#4]
DruggedBunny,

How did you insert your code (black background, green text, and formatted)?


Fryman(Posted 2012) [#5]
Oh god it was so simple

thank you very much, I had read the language reference but it just confused me even more


DruggedBunny(Posted 2012) [#6]
@DrT: use (code)Whatever(/code) but with square brackets. Use (codebox) for larger code, as it appears in a scrollable box.


DrT(Posted 2012) [#7]
DruggedBunny, Thanks for your help. I created a separate topic How to post code on this forum? and got help there also. Thanks again.