Jump to content
You are about to reply to a thread that has been inactive for 125 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Posted

My apologies in advance if this has been answered here before, but I've run into a bit of a scripting snag that searching hasn't provided a solution for so far.

Here's the scenario:

I have a string (returned from a list but I suppose that doesn't matter), and I want to conditionally execute code if the beginning of that string matches a particular bit of text.

So, for example:

-----------------------------------------------------------------------------------------------------

if (llList2String(details, 2) == "3beb1c0a-2686-4c3b-b43f-adcbb027f41d")
{

// Code to execute on match here

}

-----------------------------------------------------------------------------------------------------

The above works just fine to match and execute the code if the string is a perfect match for the hard-coded UUID (text of any kind really).

But what if I'd like to execute the code for any UUID beginning with "3beb1", no matter what the rest of it contains?

Any advice would be greatly appreciated.

Thanks in advance!

Posted

You can use llGetSubString to compare only the beginning.

if(llGetSubString(llList2String(details, 2), 0, 4) == "3beb1")  ...

As a generic user-defined function:

integer starts_with(string s, string prefix) {
    integer len_prefix = llStringLength(prefix);
    if(len_prefix)
        return prefix == llGetSubString(s, 0, len_prefix-1);
    else
        return TRUE; // empty string is always a prefix to every string
}

 

Posted (edited)

llGetSubString has the disadvantage of needing to specify the length of the starting string precisely, so it's easy to make a counting mistake and never recognize your string.

If you're not worried about efficiency and trying to match a variety of different-length starting strings, llSubStringIndex() can be quite handy:

string search= "forgetmenot";
string starts= "for";
if(0==llSubStringIndex(search,"for"))
{   llOwnerSay(search+" starts with "+starts);
}

llToLower() or llToUpper can also be used to make a 'case insensitive' search.

Edited by Quistess Alpha
You are about to reply to a thread that has been inactive for 125 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...