AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Check if string is empty (https://forums.alliedmods.net/showthread.php?t=47691)

KiLLeR123 11-24-2006 06:58

Check if string is empty
 
Would the following work?

Code:

if (equal(some_string,""))
/* Do some stuff */

Otherwise how to check if some_string is empty?

VEN 11-24-2006 07:14

Re: Check if string is empty
 
Code:

if (!strring[0])
This doesn't necessarily mean that array is fully empty.
I.e. if you have
Code:

string[4] = {0, 'h', 'i', 0}
The array isn't empty, but "hi" will not be printed:
Code:

server_print("%s", string)
because technically the content of the string is read until the nul terminator.

KiLLeR123 11-24-2006 08:45

Re: Check if string is empty
 
But as you say, some_string[0] doesn't always correspond to the length, if you assign it other values.

Still, would
Code:

if (equal(some_string,""))
work?

Or maybe I should just use
Code:

if (!strlen(some_string))

VEN 11-24-2006 09:15

Re: Check if string is empty
 
if (!strring[0])

if (!strlen(some_string))


if (equal(some_string,""))


Result will be always the same but if (!strring[0]) is most efficient.

string[4] = {0, 'h', 'i', 0} - technically this string is empty because we have nul terminator as the first element of the array.


But at the same time string[4] = {0, 'h', 'i', 0} is the array, but not empty.

If you not sure what to do or didn't get it you can tell me what you trying to achieve so i can tell you what you have to do - check the empty string or the whole array.

Hint: In the most cases for strings you will have to check the empty string (first element).

dutchmeat 11-24-2006 09:33

Re: Check if string is empty
 
sometimes the string only has a space: ' '
I think you can check it as this:


Code:
if (!strlen(some_string) < 1) //a space is one byte, length 1 ?




But this is what i think, Ven might correct me on this one, he always does ;)

jim_yang 11-24-2006 09:50

Re: Check if string is empty
 
if (!strlen(" ") < 1)
if (!strlen("a") < 1)
comes the same result: true
so you can't tell if it's a space

Simon Logic 11-24-2006 11:22

Re: Check if string is empty
 
String with a space is NOT an empty string.

But here is universal function:
Code:

stock is_empty_str(const str[], fl_spacecheck = false)
{
  new i = 0
  if(fl_spacecheck)
    while(str[i] == 32) i++
  return !str[i]
}


KiLLeR123 11-24-2006 11:33

Re: Check if string is empty
 
Quote:

Originally Posted by VEN (Post 406705)
string[4] = {0, 'h', 'i', 0} - technically this string is empty because we have nul terminator as the first element of the array.


But at the same time string[4] = {0, 'h', 'i', 0} is the array, but :lol:not empty.

If you not sure what to do or didn't get it you can tell me what you trying to achieve so i can tell you what you have to do - check the empty string or the whole array.

Hint: In the most cases for strings you will have to check the empty string (first element).

But wait, isn't null \0? It would be different from 0, wouldn't it? Sorry but I'm way too used to writing C, in fact I've never even learned Small, I just code all my AMX scripts as if they were C (for example, all my AMX code have statement-terminating semicolons :lol:). So I was a bit baffled by your some_string[0], since you could assign anything to it, and it doesn't really mean anything...I'm not sure if \0 is null in Small though, so kindly correct me if I'm hopelessly wrong here.

Actually for a function in my plugin, I need to fill up some_string with different strings, separated by commas. If some_string is empty, the function simply fills it up with the first string, if not, then it adds a comma before adding the string. Thus some_string looks like this:

1st time: string1
2nd time: string1,string2
3rd time: string1,string2,string3
......and so on

The function is called several times, as you can see, each time appending a new string, that's why it needs to check if some_string is empty. If it is, then it means it's the first time a string is added to some_string. If it's not then it knows to add a comma before adding another string to it. It seems to be taken care of with
Code:

if (!strlen(some_string))
but I simply want to learn more about this. :)

XxAvalanchexX 11-24-2006 12:01

Re: Check if string is empty
 
^ is the escape character in Small, so the equivalent to \0 is ^0. But, ^0 still equals 0, so either work.

Strings in Small are just arrays, with each index containing the character code for the character at that position. So, they read up until they get to an array index with a value of 0 (null), which marks the end of the string.

VEN 11-24-2006 13:36

Re: Check if string is empty
 
You better just check the first element of the array (string). If it's equal to zero - the string is empty. This is more efficient than calling a native function. But result is the same for both cases.

Avalanche is correct.
I'll try to describe it even more detail.

String is an array of characters: string[4] = {'^0', 'h', 'i', '^0'}
We have 4 charaters as a four elements of the array.
Each character corresponds to ASCII symbol.
ASCII symbol can be represented as a number.
So the string can be represented as a sequence of numbers.
So string[4] = {'^0', 'h', 'i', '^0'} is equal to string[4] = {0, 104, 105, 0}
In Pawn the zero character '^0' is equal to 0 and indicates the end of a string.
So our string lenght is zero (len == 0).

\ is the Pawn's default control character.
In the past the Pawn was called Small.
The Small's default control character was ^
So the AMXX compiler still uses ^ as a default control character.
The control character can be changed in your script with the #pragma ctrlchar char

Any questions?


All times are GMT -4. The time now is 07:01.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.