Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

More effective string trims?

Chaz Longstaff
Registered User
Join date: 11 Oct 2006
Posts: 685
07-15-2007 13:32
llStringTrim: Returns a string that is src with leading and/or trailing white space (spaces, tabs, and line feeds) trimmed from it.

That's kewl that it does that.

But what if someone types in a notecard:

has been directed to separate values with comma space

and by mistake s/he types:

apple[comma space]banana[comma space]pineapple[comma space space]orange

(two spaces after comma pineapple).

Looks like llStringTrim doesn't catch that.

Before I reinvent the wheel, does anyone already have a trim function that is more thorough and gets in the middle of things as well? Kinda used to that from other places.
RJ Source
Green Sky Labs
Join date: 10 Jan 2007
Posts: 272
07-15-2007 15:01
llParseString2List takes multiple delimiters to parse and remove.
So:

llParseString2List("apple, banana, pineapple, orange",[","," "],[]);

would do what you want and would return:

["apple", "banana", "pineapple", "orange"]

Even if orange was preceded by multiple spaces.
Chaz Longstaff
Registered User
Join date: 11 Oct 2006
Posts: 685
07-15-2007 16:41
>>would do what you want

almost, except it gives me a list :}

the next step i reckon is to take it out of list format and restore it to a string.
RJ Source
Green Sky Labs
Join date: 10 Jan 2007
Posts: 272
07-15-2007 16:55
This would break it all down into the components, then put it back together with no spaces:

llDumpList2String(llParseString2List("apple, banana, pineapple, orange",[","," "],[]), ",";);

would return: "apple,banana,pineapple,orange"

no spaces.
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
07-15-2007 21:06
Or, you could search for spaces and remove them.

integer space = llSubStringIndex( string_with_spaces, " " );

while( space != -1 )
{
string_with_spaces = llDeleteSubString( sting_with_spaces, space, space );
space = llSubStringIndex( string_with_spaces, " " );
}
Imajica Hand
Registered User
Join date: 3 Feb 2006
Posts: 66
07-16-2007 08:56
Both solutions are ok if you're not using multiple words in values (e.g.: banana, pineapple, passion fruit, orange).

I would split using just ",", then trimming leading and trailing spaces in each value using llStringTrim() function.

Something like that:

string sourceString = "banana, pineapple, passion fruit, orange";
list values = [];
string newValues = "";
integer length = 0;

values = llParseString2List(sourceString ,[","],[]);

length= llGetListLength(values);

for (x = 0; x < length; x++)
{
if(x>0)
newValues = newValues + ",";

newValues = newValues + llStringTrim(values, STRING_TRIM);
}

Anyway, as "," is a character that could be used as well in some cases, I usually prefer tu use uncommons characters as separator, like my beloved pipe "|" ;)
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
07-16-2007 10:21
From: Imajica Hand

Anyway, as "," is a character that could be used as well in some cases, I usually prefer tu use uncommons characters as separator, like my beloved pipe "|" ;)

The vertical bar is hard to read and too easily confused with the letter l, depending on font and screen resolution.

The bottom line is you need to reserve some character as a separator. In the case of common configuration items, comma is fine. Other alternatives are semicolon, colon, equals. Just choose one based on what's best for the users, not the programmer.
Milambus Oh
Registered User
Join date: 6 Apr 2007
Posts: 224
07-16-2007 10:29
Endline works as a great separator =)

Depending on what you are doing of course.

Just remember that you do not have to use a single character as your separator if it makes it easier to understand by using two.
Imajica Hand
Registered User
Join date: 3 Feb 2006
Posts: 66
07-16-2007 14:04
I agree with you.
Anyway, with proper instructions (SL Scripted Object should always include well made instructions), the vertical bar will be not a problem.

In general cases, comma its ok, but in most of my script, I need to have the widest range of available characters (e.g.: vendor with items descriptions), so, vertical bar (I'm used to call it pipe) is the best compromise for my purposes. Of course, as I've wrote below, I try to include clear instructions about that.
After all, this is the best for the users as well, leaving them the chance to write almost everyting they want.

Oops... we are OT :o

From: Kidd Krasner
The vertical bar is hard to read and too easily confused with the letter l, depending on font and screen resolution.

The bottom line is you need to reserve some character as a separator. In the case of common configuration items, comma is fine. Other alternatives are semicolon, colon, equals. Just choose one based on what's best for the users, not the programmer.
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
07-17-2007 08:32
From: Imajica Hand
I agree with you.
Anyway, with proper instructions (SL Scripted Object should always include well made instructions), the vertical bar will be not a problem.

One of the aphorisms of Usability is that instructions are a crutch to compensate for poor usability. It's an exaggeration, of course, but it's something to be aware of.

But in this case, instructions won't solve the problem. Better fonts in the notecard editor, an adjustable font size for notecards, a better display, new glasses, etc. are things that might help.

From: someone

In general cases, comma its ok, but in most of my script, I need to have the widest range of available characters (e.g.: vendor with items descriptions), so, vertical bar (I'm used to call it pipe) is the best compromise for my purposes.

The term pipe is so tied to shell scripting, primarily UNIX scripting, that I avoid it. Developers here may recognize it, but the end users for configuration settings are like to trip over it.
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
07-17-2007 08:36
From: Imajica Hand
Both solutions are ok if you're not using multiple words in values (e.g.: banana, pineapple, passion fruit, orange).
...
values = llParseString2List(sourceString ,[","],[]);

length= llGetListLength(values);

for (x = 0; x < length; x++)
{
if(x>0)
newValues = newValues + ",";

newValues = newValues + llStringTrim(values, STRING_TRIM);
}

Getting back to the original question, I find myself doing something essentially similar to this. However, what I'd really like would be something like

values = llParseString2List(sourceString ,[","],[], STRING_TRIM);

so that the llParseString2List function could be told to trim the items it finds before inserting them into the list it generates. Better yet, add an option to both this and llStringTrim to compress embedded white space, in addition to the other trimming.