About Monkey 2 › Forums › Monkey 2 Programming Help › String.To Chars queston
This topic contains 6 replies, has 4 voices, and was last updated by
EdzUp
1 year, 9 months ago.
-
AuthorPosts
-
June 25, 2017 at 2:37 pm #8914
MX1 had String.ToChar and String.ToChars to convert a string to a char array, MX2 has a C string thing in the String class but it has a Void Ptr and I can’t see any info on Void Ptr I’ve tried casting to no avail.
Anyone have any idea how to convert a string to a array of chars?
Thanks in advance
June 25, 2017 at 3:10 pm #8917would this work for you?:
[/crayon]Monkey1234567891011121314151617[crayon-5cba87f882658148518808 inline="true" ]#Import "<std>"Using std..Function Main()Local b:Int[] = New int[5]Local a:String = "Hello World"b[0] = a[0]b[1] = a[1]b[2] = a[2]b[3] = a[3]b[4] = a[4]Print String.FromChars(b)EndJune 25, 2017 at 3:36 pm #8918Ah thanks Jesse didn’t know it was that easy to do
June 26, 2017 at 3:10 am #8932@edzup How do you want to use chars array?
You can access to chars by index operator [ ], but it return INT not CHAR.
Example:
Monkey12345678Local str:="Hello, world!"Local latinCount:=0For Local i:=0 Until str.LengthIf (str[i] >= "A"[0] And str[i] <= "Z"[0]) Or (str[i] >= "a"[0] And str[i] <= "z"[0]) Then latinCount+=1NextPrint "latinCount: "+latinCountPrint "non-latinCount: "+(str.Length-latinCount)June 27, 2017 at 7:19 pm #8960I was trying to do something like this:
Monkey1234567891011Field FontString:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"Method SetChar:Int( Pos:Int, Chr:Int )If ( Pos >FontString.Length )Return( -1 )EndifFontString[ Pos ] = ChrReturn( 0 )EndIm getting ‘Error : Value ‘Self.FontString[Pos]’ is not assignable’ on the FontString[ Pos ] = Chr line, from the posts above I thought this was the way to use it but it doesnt seem to work the same as Monkey1 is there something im missing?
June 27, 2017 at 10:11 pm #8966Strings in monkey2 are ‘immutable’: they cannot be modified. To do what you’re trying to do you’ll need to build a new string.
June 27, 2017 at 11:18 pm #8973ah okay thanks I will recode to use slices
-
AuthorPosts
You must be logged in to reply to this topic.