Common String Functions

Written by: Headmaster


This is a list of useful functions for string manipulation. Most of these are lsl versions of the built-in VB string functions with some enhancements. Enjoy.



See comments before each function for usage!

 

////////////////////////////////////////////////////////////////////////////
//           GarrMe Dagger's Common String Functions v.10.13.07           //
//                    Copyright (c) 2007 Mark Timlin                      //
////////////////////////////////////////////////////////////////////////////
//                                                                        //
//  This program is free software: you can redistribute it and/or modify  //
//  it under the terms of the GNU General Public License as published by  //
//  the Free Software Foundation version 3 of the License.                //
//                                                                        //
//  This program is distributed in the hope that it will be useful,       //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of        //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     //
//  General Public License for more details - http://www.gnu.org/licenses //
//                                                                        //
////////////////////////////////////////////////////////////////////////////


// Returns a new string containing the leftmost length characters of source.
string StringLeft(string source, integer length)
{
    return llGetSubString(source, 0, length - 1);
}

// Returns a new string containing the rightmost length characters of source.
string StringRight(string source, integer length)
{
    return llGetSubString(source, llStringLength(source) - length, -1);
}

// Returns a new string containing the reverse of source.
string StringReverse(string source)
{
    string str;
    integer x;
    integer len = llStringLength(source);
    for (x = 1; x <= len; x++)
        str += llGetSubString(source, len - x, len - x);
    return str;
}

// Returns the index in source where pattern first appears beginning from the end of source.
// Returns -1 if no match is found.
integer SubStringIndexReverse(string source, string pattern)
{
    string str = source;    
    integer len = llStringLength(pattern);
    integer rval = llSubStringIndex(source, pattern);
    integer last = rval;
    while (rval > -1)
    {
        str = llGetSubString(str, rval + len, -1);
        rval = llSubStringIndex(str, pattern);
        if (rval > -1)
            last = last + len + rval;
    }
    rval = last;
    return rval;
}

// Returns a new string with spaces removed from the beginning of source
string StringLeftTrim(string source)
{
    integer x = 0;    
    string str = llGetSubString(source, 0, 0);
    while (str == " ")
    {
        str = llGetSubString(source, x, x);
        ++x;    
    }
    return llGetSubString(source, x - 1, -1);
}

// Returns a new string with spaces removed from the end of source
string StringRightTrim(string source)
{
    integer x = -1;    
    string str = llGetSubString(source, -1, -1);
    while (str == " ")
    {
        --x;        
        str = llGetSubString(source, x, x);
    }
    return llGetSubString(source, 0, llStringLength(source) + x);
}

// Returns a new string with number spaces added to beginning of source.
string StringSpace(string source, integer number)
{
    string str = "";
    integer x;
    for (x = 0; x < number; x++)
    {
        str += " ";    
    }
    return str + source;
}

// Returns 1 (TRUE) if source is a number, otherwise returns 0 (FALSE).
integer StringIsNumeric(string source)
{
    integer rval = 0;
    if ((float)source >= 1.175494351E-38)
        rval = 1; 
    return rval;
}

// Returns a new string replacing all instances of old with new starting at pos in source.
// A pos of 0 would begin replacing old with new at the beginning of source.
// A pos of 10 would begin replacing old with new at the 10th character of source.
string StringReplace(integer pos, string source, string old, string new)
{
    string str = source;
    string pre = "";
    if (pos > 0)
    {
        pre = llGetSubString(source, 0, pos - 1);
        str = llGetSubString(source, pos, -1);
    }
    integer len = llStringLength(old);
    integer index = llSubStringIndex(str, old);
    while (index > -1)
    {
        str = llInsertString(llDeleteSubString(str, index, index + len - 1), index, new);
        index = llSubStringIndex(str, old);
    }
    return pre + str;
}

// Returns a new string containing the characters between start and end starting at pos in source.
// A pos of 0 would begin looking for start at the beginning of source.
// A pos of 10 would begin looking for start at the 10th character of source.
// Returns "" on error.
string StringBetween(integer pos, string source, string start, string end)
{
    if (pos > 0)
        source = llGetSubString(source, pos, -1);
    integer sindex = llSubStringIndex(source, start);
    integer eindex = llSubStringIndex(source, end);
    if (sindex == -1 || eindex == -1)
        return "";
    else return llGetSubString(source, sindex + llStringLength(start), eindex -1);
}

 

Category: