Jump to content
Sylvia Wasp

how can I tell if a string is empty?

Recommended Posts

Hello, 

I'm trying to write some code that checks a string variable to see if it's empty.  I realise this is a bit goofy, but I'd still like to know how to do it.  

LSL script library is of no help as usual due to it's impenetrable language, but there are two ways that seem to be implied: 

- it seems easy to check string length and presumably an empty string would have a length of "<1" ? 

- there are some vague implications on some of the pages of the LSL library that an empty string is equal to a "NULL_KEY"  (00000000-0000-0000-0000-000000000000) ?

or is it as simple as:

if string == ""? 

I want something like: 

if (<string name> is empty) {
     do stuff;
	 }

It would also maybe be good to check the negative?  Like "if string isn't empty"??

As you can tell I've become lost on a teeny tiny detail here, lol

Sylvia

Share this post


Link to post
Share on other sites

try

if (TEST=="")

if that does not work, 

if (llStringLength(TEST)==0)

will do, with TEST being your string variable name

 

PS: OK, it is a freakin C dialect, so it might still not work, in that rare case try:

integer TESTL= llStringLength(TEST);

if (TESTL==0)

should not be neccesary but it is a trick to remember in some languages ;)

 

Edited by Fionalein
  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites
18 minutes ago, Sylvia Wasp said:

there are some vague implications on some of the pages of the LSL library that an empty string is equal to a "NULL_KEY"  (00000000-0000-0000-0000-000000000000) ?

Only when dealing with UUID keys. The variable types KEY and STRING are closely related but not completely interchangeable in every case. In functions that require or return a key,  you can use NULL_KEY or "" or even (I presume) "00000000-0000-0000-0000-000000000000" and they should be equivalent. Strings are automatically cast into keys in functions that expect keys and variables of key type.

When dealing with regular strings, using NULL_KEY will probably give you the exact string contents as listed on the wiki  page, i.e. "00000000-0000-0000-0000-000000000000" which is most definitely not an empty string.

  • Like 3

Share this post


Link to post
Share on other sites

Yes, keys are strings (kind of) and an uninitialized key or an uninitialized string is an empty string wich is not the same as a NULL_KEY.

 

  • Like 1

Share this post


Link to post
Share on other sites
On 12/7/2018 at 7:00 AM, Fionalein said:

try


if (TEST=="")

if that does not work, 

if (llStringLength(TEST)==0)

will do, with TEST being your string variable name

 

PS: OK, it is a freakin C dialect, so it might still not work, in that rare case try:


integer TESTL= llStringLength(TEST);

if (TESTL==0)

should not be neccesary but it is a trick to remember in some languages ;)

 

Thanks for all the help everyone.  I found this one the most useful.  

I realised after I posted that something along the lines of this, and the negative version:  

if ( TEST_string != "" ) {

    }

was what is simplest and what I want.  

The clarification (below) on the NULL_KEY was also most helpful 

Sylvia

Share this post


Link to post
Share on other sites
1 hour ago, Sylvia Wasp said:

I realised after I posted that something along the lines of this, and the negative version:  


if ( TEST_string != "" ) {

    }

was what is simplest and what I want.  

That returns TRUE and the  code between the braces (curly brackets) executes if TEST_string contains at least one character.

If that's what you want, then it might be safest to test for

if(llStringTrim(TEST_string,STRING_TRIM) !=""){//first ensure there are no spaces or non-printing characters to confuse matters
	//do stuff
}

 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×