Raised This Month: $51 Target: $400
 12% 

Check if string is empty


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
KiLLeR123
New Member
Join Date: Nov 2006
Old 11-24-2006 , 06:58   Check if string is empty
Reply With Quote #1

Would the following work?

Code:
if (equal(some_string,""))
/* Do some stuff */
Otherwise how to check if some_string is empty?
KiLLeR123 is offline
VEN
Veteran Member
Join Date: Jan 2005
Old 11-24-2006 , 07:14   Re: Check if string is empty
Reply With Quote #2

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.

Last edited by VEN; 11-24-2006 at 07:18.
VEN is offline
KiLLeR123
New Member
Join Date: Nov 2006
Old 11-24-2006 , 08:45   Re: Check if string is empty
Reply With Quote #3

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))
KiLLeR123 is offline
VEN
Veteran Member
Join Date: Jan 2005
Old 11-24-2006 , 09:15   Re: Check if string is empty
Reply With Quote #4

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).

Last edited by VEN; 11-24-2006 at 09:17.
VEN is offline
dutchmeat
Senior Member
Join Date: Sep 2006
Old 11-24-2006 , 09:33   Re: Check if string is empty
Reply With Quote #5

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 ;)
__________________
before you criticize someone, you should walk a mile in their shoes. that way, when you criticize them, you're a mile away and you have their shoes.
dutchmeat is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 11-24-2006 , 09:50   Re: Check if string is empty
Reply With Quote #6

if (!strlen(" ") < 1)
if (!strlen("a") < 1)
comes the same result: true
so you can't tell if it's a space
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
Simon Logic
Senior Member
Join Date: Nov 2006
Location: RF
Old 11-24-2006 , 11:22   Re: Check if string is empty
Reply With Quote #7

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]
}
Simon Logic is offline
Send a message via Skype™ to Simon Logic
KiLLeR123
New Member
Join Date: Nov 2006
Old 11-24-2006 , 11:33   Re: Check if string is empty
Reply With Quote #8

Quote:
Originally Posted by VEN View Post
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).
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 ). 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.
KiLLeR123 is offline
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 11-24-2006 , 12:01   Re: Check if string is empty
Reply With Quote #9

^ 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.
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX is offline
VEN
Veteran Member
Join Date: Jan 2005
Old 11-24-2006 , 13:36   Re: Check if string is empty
Reply With Quote #10

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?
VEN is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:54.


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