Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active December 26, 2015 10:09
Show Gist options
  • Save zeffii/7135059 to your computer and use it in GitHub Desktop.
Save zeffii/7135059 to your computer and use it in GitHub Desktop.

functions for string objects

things i'm missing:

  • class 'string' has no member 'split'

this is a temporary splitter

fun string[] split(string arr, string token){
    // you trim arr before feeding it to this function
    string return_array[0];
    string collect;
    string ch;
    for(0 => int i; i<arr.length(); i++){
        arr.substring(i, 1) => ch;
        if (ch == token){
            return_array << collect.substring(0);
            "" => collect;
        } 
        else { ch +=> collect; }
    }
    return return_array << collect;
}

.substring

.substring(int pos)  
.substring(int pos , int len)  

(1) return new string with characters from pos to end of string
(2) return new string with characters from pos of length len

.replace

.replace(int pos, string str)
.replace(int pos, int len, string str)    

(1) replace characters of string at pos with str
(2) replace len characters of string with str, starting at pos

.find

.find(int ch)  
.find(int ch, int pos)  
.find(string str)  
.find(string str, int pos)  

(1) search for character ch in string, return index of first instance
(2) search for character ch in string, return index of first instance at or after index pos
(3) search for string str in string, return index of first instance
(4) search for string str in string, return index of first instance at or after index pos

.rfind

.rfind(int ch)  
.rfind(int ch, int pos)  
.rfind(string str)
.rfind(string str, int pos)

(1) search for character ch in string, return index of last instance
(2) search for character ch in string, return index of last instance at or before index pos
(3) search for string str in string, return index of last instance
(4) search for string str in string, return index of last instance at or before index pos

misc: .charAt, .setCharAt, .insert, .erase

.charAt(int index)
return character of string at index

.setCharAt(int index, int ch)
set character of string at index to ch

.insert(int pos, string str)
insert str at pos

.erase(int pos, int len)
remove len characters from string, beginning at pos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment