Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

need help with string snippet

Micheal Moonlight
Registered User
Join date: 4 Sep 2005
Posts: 197
10-25-2005 22:47
I'm looking for a small snippet, that would take in a string, and if it's longer then 200 characters split it into 2, without causing any words to be broken between the two... (so it looks for the first space backwards from position 200, then uses that spot to split the string into 2)

Any help would be greatly appreciated.
Travis Bjornson
Registered User
Join date: 25 Sep 2005
Posts: 188
10-25-2005 23:21
I think this can be done pretty easily, but it might take a little CPU time if there are a lot of spaces. I don't see a function to search backwards, so I would make it search forward until it goes past the 200th character, then back up. Also need a maximum number of words as a sanity check to avoid infinite loops. 200 words would probably be fine.

I'll give it a shot tomorrow (Wed) if I have some time.
Micheal Moonlight
Registered User
Join date: 4 Sep 2005
Posts: 197
10-25-2005 23:26
was actually thinking on it after i posted that, and think the easiest way to do it is moving the string to a list, then one by one adding the words in the list to a temp string until it's gone past 200 length. then put the rest into the second string. Not sure if this is the most efficiant way of doing it, but it would work.
Travis Bjornson
Registered User
Join date: 25 Sep 2005
Posts: 188
10-25-2005 23:39
Good idea, but I don't think the lists are necessary. Add words to a second string, checking first to see if each new word will push the second string over 200 char, then start adding to a third string.
RyeDin Meiji
Reluctant Entrepeneur
Join date: 15 Mar 2005
Posts: 124
10-26-2005 17:59
Untested code... but general idea should work... (may have to add 1 or subtract one from the final GetSubString indexes... )

CODE

string mainString = "blah blah blah... ";
string secondaryString;

integer stringLength = llGetStringLength( mainString );

if ( stringLength > 200 )
{
integer foundSpace = FALSE;
integer spaceNum = 200;
while ( foundSpace == FALSE )
{
secondaryString = llGetSubString( mainString, spaceNum, spaceNum );
if ( secondaryString != " " )
--spaceNum;
else
foundSpace = TRUE;
}
secondaryString = llGetSubString( mainString, spaceNum, stringLength);
mainString = llGetSubString( mainString, 0, spaceNum - 1);
}


EDIT: I changed the order of the last 2 lines... changing the value of mainString 1st was just plain silly :)
_____________________
if (!you)
{
who();
}
Ben Bacon
Registered User
Join date: 14 Jul 2005
Posts: 809
10-27-2005 04:54
ignore this post - RyeDin's method works just fine.
You were right, and I was wrong.
You can sing the "I was right" song


Michael - how often would you want to do this? If it is gonna be a once-off or very infrequent thing then RyeDin's solution is the easiest to understand and maintain.
But - if you need to do it often, you'll find that you need something more efficient - albeit more complex.

The average length of an English word is 6-7 letters - so think of taking a substring from, say, position 180 to position 200. It should contain 3 or 4 spaces. Find the last one in that small string rather than scanning through 200 chars. If you can't find a space there - try 160-180 and so on.
RyeDin Meiji
Reluctant Entrepeneur
Join date: 15 Mar 2005
Posts: 124
10-27-2005 06:18
Ben, not sure why my method wouldn't be all-purpose (put into a function)?

It starts at 200 and goes back... like you said at most 5 or 6 times on average. It doesn't scan through 200 chars as you say... Can you expound?
_____________________
if (!you)
{
who();
}
Ben Bacon
Registered User
Join date: 14 Jul 2005
Posts: 809
10-27-2005 06:36
Rye - can I blame it on a severe lack of coffee? :D
RyeDin Meiji
Reluctant Entrepeneur
Join date: 15 Mar 2005
Posts: 124
10-27-2005 06:55
LOL... hmm ok let me try to remember the words to the "I was right" song. It's been such a very long time since I've been able to sing it, what with being married and all :D
_____________________
if (!you)
{
who();
}