Geas MUD, enter the Aventure!
Geas Home | Play the Game

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3.9 Array/string conversion

Very often you come to situations where you either have a string that you would like to break up into smaller strings based on a regular substring, or conversely where you have a number of strings you would like to paste together to make up one single string. For this purpose you can use the efuns explode() and implode().

The efun explode() takes two two strings as arguments, the first is the string you want to break up, and the second is the pattern that explode() looks for as a marker of where to break the string. It returns an array of strings holding the result. The efun implode() takes an array and a string as arguments, returning one string made up from the contents of the array with the string argument pasted in between all elements.

string *explode(string str, string expstr)
string implode(string *strlist, string impstr)
    string fruit = "apple and banana and pear " + 
                   "and orange and fatty eating it all";
    string *fruit_list;

    fruit_list = explode(fruit, " and ");

    /* The result is:
        [0] = (string) "apple"
        [1] = (string) "banana"
        [2] = (string) "pear"
        [3] = (string) "orange"
        [4] = (string) "fatty eating it all"

     fruit = implode(fruit_list, ", ");
     write(fruit + "\n");

    // The result is:
    // apple, banana, pear, orange, fatty eating it all

This document was generated by Ronny Wikh on July, 8 2003 using texi2html