Strcmp
From SA-MP Wiki
(Difference between revisions)
Revision as of 00:53, 12 February 2015 Kar (Talk | contribs) (typo...) ← Previous diff |
Revision as of 07:43, 11 May 2015 MP2 (Talk | contribs) Next diff → |
||
Line 13: | Line 13: | ||
{{Returns|'''0''' if strings match each other on given length;<br />'''1''' o r '''-1''' if some character do not match: string1[i] - string2[i] ('i' represents character index starting from 0);<br />'''difference in number of characters''' if one string matches only part of another string.}} | {{Returns|'''0''' if strings match each other on given length;<br />'''1''' o r '''-1''' if some character do not match: string1[i] - string2[i] ('i' represents character index starting from 0);<br />'''difference in number of characters''' if one string matches only part of another string.}} | ||
- | {{Note2|This function also returns 0 if either string is empty.}} | + | {{Notes2| |
+ | * This function returns 0 if either string is empty. Check for null strings with isnull(). If you do not, for example, people can login to anyone's account by simply entering a blank password. | ||
+ | * If you compare strings from a text file, you should take in to account the 'carriage return' and 'new line' special characters (\r \n), as they are included, when using [[fread]]. | ||
+ | }} | ||
- | Good examples of using strcmp can be found at the [[Using strcmp()|Using strcmp()-Tutorial.]] | + | {{Example}} |
+ | <pawn> | ||
+ | new string1[] = "Hello World"; | ||
+ | new string2[] = "Hello World"; | ||
+ | // Check if the strings are the same | ||
+ | if(!strcmp(string1, string2)) | ||
- | '''NOTE:''' | + | new string3[] = "Hell"; |
- | If you compare string from a text file, you should care about \r\n 'line return' and 'new line' special characters, as they are included, when using [[fread]]. | + | // Check if the first 4 characters match |
+ | if(!strcmp(string2, string3, false, 4)) | ||
+ | // Check for null strings with isnull() | ||
+ | if(!strcmp(string1, string2) && !isnull(string1) && !isnull(string2)) | ||
+ | |||
+ | // Definition for isnull(): | ||
+ | #if !defined isnull | ||
+ | #define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1])))) | ||
+ | #endif | ||
+ | |||
+ | </pawn> | ||
+ | |||
+ | Good examples of using strcmp can be found at the [[Using strcmp()|Using strcmp()-Tutorial.]] | ||
{{RelatedFunctions}} | {{RelatedFunctions}} |
Revision as of 07:43, 11 May 2015
strcmp
Note: This function name starts with a lowercase letter. |
Description:
Compares two strings to see if they are the same.
(const string1[], const string2[], bool:ignorecase, length)
string1 | The first string to compare. |
string2 | The second string to compare. |
ignorecase (optional) | When set to true, the case doesn't matter - HeLLo is the same as Hello. When false, they're not the same. |
length (optional) | When this length is set, the first x chars will be compared - doing "Hello" and "Hell No" with a length of 4 will say it's the same string. |
Return Values:
0 if strings match each other on given length;
1 o r -1 if some character do not match: string1[i] - string2[i] ('i' represents character index starting from 0);
difference in number of characters if one string matches only part of another string.
1 o r -1 if some character do not match: string1[i] - string2[i] ('i' represents character index starting from 0);
difference in number of characters if one string matches only part of another string.
Important |
|
Example Usage:
new string1[] = "Hello World"; new string2[] = "Hello World"; // Check if the strings are the same if(!strcmp(string1, string2)) new string3[] = "Hell"; // Check if the first 4 characters match if(!strcmp(string2, string3, false, 4)) // Check for null strings with isnull() if(!strcmp(string1, string2) && !isnull(string1) && !isnull(string2)) // Definition for isnull(): #if !defined isnull #define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1])))) #endif
Good examples of using strcmp can be found at the Using strcmp()-Tutorial.
Related Functions
The following functions may be useful, as they are related to this function in one way or another.
- strfind: Search for a string in another string.
- strtok: Get the next 'token' (word/parameter) in a string.
- strdel: Delete part of a string.
- strins: Insert text into a string.
- strlen: Get the length of a string.
- strmid: Extract part of a string into another string.
- strpack: Pack a string into a destination string.
- strval: Convert a string into an integer.
- strcat: Concatenate two strings into a destination reference.