Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Splitting Avatar Names at whitespace

RaH Wollongong
Registered User
Join date: 11 Jul 2006
Posts: 19
12-20-2006 21:56
Is there a function in the lsl similar to Pythons split or strip or something to be precise can strip everything after and including a whitespace from a string?

I'm sure I could create a buffer to store the name in calculate the size of each char in the string then remove everything after the whitespace, but that seems like reinventing the wheel.

Anyone know of something like this. I was unable to find anything in the Wiki.
OneBigRiver Stork
Diversity matters
Join date: 29 Mar 2006
Posts: 44
12-20-2006 22:10
From: RaH Wollongong
Is there a function in the lsl similar to Pythons split or strip or something to be precise can strip everything after and including a whitespace from a string?

I'm sure I could create a buffer to store the name in calculate the size of each char in the string then remove everything after the whitespace, but that seems like reinventing the wheel.

Anyone know of something like this. I was unable to find anything in the Wiki.


Check out llParseString2List ... this might do what you need.
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
12-21-2006 05:46
llSubStringIndex to find the whitespace llGetSubString to strip it

ie
CODE


integer stop = llSubStringIndex("joe av"," ");
string output = llGetSubString("joe av",0,(stop - 1));


or
CODE

output = llGetSubString("joe av",0,(llSubStringIndex("joe av"," ") - 1));


you want the - 1 or else it would save the space in the string...
Learjeff Innis
musician & coder
Join date: 27 Nov 2006
Posts: 817
12-21-2006 09:59
llParseString2List() is pretty much equivalent to Python split().

CODE

list words;
string first_name;
string str = "Learjeff Innis";

words = llParseStringList(str, " ", ""); // words = str.split(" ");
first_name = llList2String(words, 0); // first_name = words[0];


first_name's value is then "Learjeff".

Main difference is "words[0]" would raise an error in Python if str was empty, but simply returns the empty string in LSL (IIRC).
RaH Wollongong
Registered User
Join date: 11 Jul 2006
Posts: 19
12-21-2006 18:39
Thank you that makes a world of difference, and does exactly what I wanted.
Mevo Syaka
Ganja Guru
Join date: 12 Dec 2006
Posts: 33
04-04-2007 19:24
From: Osgeld Barmy
llSubStringIndex to find the whitespace llGetSubString to strip it

ie
CODE


integer stop = llSubStringIndex("joe av"," ");
string output = llGetSubString("joe av",0,(stop - 1));


or
CODE

output = llGetSubString("joe av",0,(llSubStringIndex("joe av"," ") - 1));


you want the - 1 or else it would save the space in the string...


This is very helpul but how would I get the end part too without knowing what the second word is?
_____________________
FourTwenty Headshop



Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
04-04-2007 21:57
From: Osgeld Barmy
you want the - 1 or else it would save the space in the string...
In similar circumstances, I tend to favor llDeleteSubString, to skip the additional math:

CODE

string Name = "Joe Blo";
integer Space = llSubStringIndex( Name, " " );
string FirstName = llDeleteSubString( Name, Space, -1 );
string LastName = llDeleteSubString( Name, 0, Space );
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
04-04-2007 22:56
CODE

string input = "joe av";

default
{
state_entry()
{
string first_name = llGetSubString(input ,0 ,(llSubStringIndex(input ," ") - 1));
string last_name = llGetSubString(input ,(llSubStringIndex(input ," ") + 1), -1);
}
}


let me inflate this so you can see whats actually going on ....
CODE

string input = "Osgeld Barmy";

default
{
state_entry()
{
// find the space in the name
// and save its numerical position
// within the input string
integer whitespace_pointer = llSubStringIndex(input, " ");

string first_name = llGetSubString(input ,0 , (whitespace_pointer - 1) );
// get the first name with llGetSubString
// starting at the first charater (0) and
// ending at the whitespace_pointer minus 1
// minus 1 so we dont get the whitespace in
// the output string

string last_name = llGetSubString(input, (whitespace_pointer + 1), -1);
// get the last name with llGetSubString
// starting at the first charater AFTER
// the whitespace (whitespace_pointer + 1)
// and continue reading the string untill
// we reach the end, to do this use (negitive) 1

llOwnerSay("the whitespace is at " + (string)whitespace_pointer);
llOwnerSay("the first name is " + first_name);
llOwnerSay("the last name is " + last_name);
}
}


simple eh?

http://lslwiki.net/lslwiki/wakka.php?wakka=llGetSubString
http://lslwiki.net/lslwiki/wakka.php?wakka=llSubStringIndex
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
04-04-2007 23:01
nm heh