String Manipulations

Started by thepenguin, July 20, 2013, 08:03:07 AM

Previous topic - Next topic

thepenguin

Is there any way to do these?

String.charAt(index), et cetera. (of course as CRPL equivalents)

Alternately, I'd want to turn a string into a list of chars, and then these functions can be done that way. (ToCharList perhaps?)

This way, I can process a string letter by letter.
We have become the creeper...

knucracker

I can add the CharAt function.  I may also add a few others right fast...

J

Do you want to build your own language into a level so the player can manipulate it? :P


I'm not going to mess with input stuff unless I really need it for a map.
A function that can put each character of a string on the stack as a string would be the best I think. If you are are searching for a number in a string you can easily pop all letters from the stack (and use concat later to make it one number again)

knucracker

Substring, StartsWith, EndsWith, Split, StringToList
These are the one I just added.  That (along with concat) probably covers 99% of the most common use cases.  Any others, there is StringToList.


Now Split and StringToList both return 'lists'.  These would be the first APIs to do that.  A list is just a single item pushed to the stack, you have to use the list apis to deal with it.

So you can do something like this pointless example:

Split("Four Score and Seven Years Ago" " ") ->words
do(GetListCount(<-words) 0)
   StringToList(GetListElement(<-words I)) ->letters
   do(GetListCount(<-letters) 0)
      trace(GetListElement(<-letters I))
   loop
loop



The first line splits a string into substrings based on a " " ( a space).  The results are put in a list called 'words'.  The outer loop goes over each element of the word list.  For each word, the letters are split to a list called 'letters'.  That letters list is then looped over and each letter is printed out.

This could all have been done on the stack of course, but using lists makes it easier to hold onto 'words' and 'letters' for later use.

thepenguin

We have become the creeper...