About Monkey 2 › Forums › Monkey 2 Development › String.Chars() / .LTrim() / .RTrim() / .LSet() / .RSet() / .Space()
This topic contains 2 replies, has 3 voices, and was last updated by
Mark Sibly
2 years, 8 months ago.
-
AuthorPosts
-
August 1, 2016 at 4:18 pm #2654
What about adding a method/property .Chars() to the string class,
to get an array with all characters?
Can’t see type ‘Char’ in the docs at Monkey Types
so I guess .Chars() would return an array of type UShort or UInt (Unicode Chars).Maybe .CharAt(index) would also make sense. Left() and Right() return a string,
but what about something like Asc() to get the first character of a string as a number?
Or is [index] supported with strings? Can’t find it in the docs.Would it make sense to add an optional character or string argument to
String.Trim() for removing other things, not only ‘whitespace’?
Default could be to remove space/whitespace, so ” 1 “.Trim() returns “1”,
and “…1…”.Trim(“.”) returns “1” as well.
And methods LTrim() and RTrim() to remove it only on one side? Like VisualBasic.VB has also LSet() and RSet() for string padding. That often makes sense
if you want to display numbers, so you can do:Monkey1"1".RSet(5) to get " 1"or
Monkey1"1".RSet(5,"0") to get the result "00001"Padding is also often used when displaying Hex numbers: “F” > “0F” or “000F” etc.
And String.Space(count) to create an empty string of a specific length,
or something like VB’s StrDup(Count, Char) to create a string filled with
a specific char?August 1, 2016 at 5:48 pm #2657you can do this to get the ascii code:
Print “text”[0] ‘ will print the ascii for “t”
August 1, 2016 at 6:47 pm #2661I agree that String could do with some more methods, though I’m not too keen on the ‘L’ ‘R” names – how about…?
Dup:String( num:Int )
TrimStart:String()
TrimEnd:String()
Pad:String( len:Int,padding:String=” ” ) ‘centers string…? Symmetry with Trim()…
PadStart:String( len:Int,padding:String=” ” )
PadEnd:String( len:Int,padding:String=” ” )
I’m thinking ‘Start’ ‘End’ instead of ‘Left’ ‘Right’ here to go with StartsWith and EndsWith, but ‘Left’ and ‘Right’ would also work.
Not quite sure how padding:String works – Duped as much as necessary? – but it’d be nice to allow for more than just padding with spaces.
I did consider allowing String * Int for Dup, but with the relaxed type conversions was a bit worried it could too easily trip people up. Still an option though…?
-
AuthorPosts
You must be logged in to reply to this topic.