Check if string is empty
Would the following work?
Code:
if (equal(some_string,"")) |
Re: Check if string is empty
Code:
if (!strring[0])I.e. if you have Code:
string[4] = {0, 'h', 'i', 0}Code:
server_print("%s", string) |
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,""))Or maybe I should just use Code:
if (!strlen(some_string)) |
Re: Check if string is empty
if (!strring[0]) if (!strlen(some_string)) if (equal(some_string,"")) Hint: In the most cases for strings you will have to check the empty string (first element).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. |
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 ;) |
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 |
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) |
Re: Check if string is empty
Quote:
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)) |
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. |
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.