Strcmp

From SA-MP Wiki

Jump to: navigation, search

Strcmp


strcmp can be used to compare whetever two strings are the same, but has (as all functions), problems with cyrillic characters. Another problem of this function is, if one string or both is empty, strcmp will return 0 (same when strings are the same).


Parameters:
(const string1[], const string2[], bool:ignorecase, length)
string1The first string to compare.
string2The 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.


Returns0 if the strings are the same.


Good examples of using strcmp can be found at the Using strcmp()-Tutorial.


NOTE:

If you don't and compare one string from a textfile with an other variable strcmp will not return 0 (only if length is larger then the string in the file)

This is because the string in the file has \r\n for the line return at the end, so if you use strcmp to compare textfile strings do it like this:

strcmp(stringfromvariable,stringfromtextfile,true/false, getlength(stringfromvariable))

so the \r\n in your file will be ignored because it is out of the range to compare!


Related Functions

The following functions might be useful as well, as they're related to this function in one way or another.

Any functions here can also be found on the Old scripting functions page.

  • strfind: Search for a substring in a string.
  • strtok: Search for a variable typed after a space.
  • strdel: Delete part/all of a string.
  • strins: Put a string into another string.
  • strlen: Check the length of a string.
  • strmid: Extract characters from a string.
  • strpack: Pack a string into a destination.
  • strval: Find the value of a string.
  • strcat: Concat two strings into a destination reference.
Personal tools