Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Adding n times " " to a string...Possible or no?

Darko Lednev
Registered User
Join date: 20 Aug 2007
Posts: 31
08-27-2007 10:25
I'm trying to add a space (" ";) n times to a detected name (string). I want to add certain amount of spaces after the detected name so the new string is long exacty 25 characters. This doesn't work:

integer maxNameLength = 25;
string finalName;

default
{
state_entry()
{

}
touch_start(integer total_number)
{
finalName = llDetectedName() + ((maxNameLength - llStringLength(llDetectedName())) * " ";);
llSay(0, finalName);
}
}

Does anybody has any idea how can I add those spaces after detected name to fill up to 25 characters?
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
08-27-2007 10:46
I use a list of predefined spaces. The list index is the same as the number of spaces needed. (the '_' symbol represents a space below, but it's just a space)

string test = "needs 6 more spaces";
list spaces = ["", "_", "__", "___", "____", "_____", "______", "_______"];

then, I figure how many spaces I need and add with the number as the index:
integer shortage = 25 - llStringLength(test);
test = test + llList2String(spaces, shortage);

You could also use a loop, but I found it to be slower.
Darko Lednev
Registered User
Join date: 20 Aug 2007
Posts: 31
08-27-2007 10:50
This is great! Thanks a lot, you saved me.
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
08-27-2007 10:57
string SPACES = " type a bunch of spaces here ok ";
string pad(string src, integer len)
{
integer src_len = llStringLength(src);
if ( src_len >= len ) return src;
return src + (llDeleteSubString(SPACES, len - src_len, -1));
}
Darko Lednev
Registered User
Join date: 20 Aug 2007
Posts: 31
08-27-2007 12:00
Oh this is even better. Thanks alot DoteDote and Masakazu