Understanding Strings

Introduction
I've made this tutorial for the newer people that don't understand the basic concepts of strings. There is quite a few topics with people very confused on why they cant just compare two strings together with the equals to in PAWN. I'm pretty much going to discuss what strings are and why you cant do that.

What are strings?
Strings are multi-celled variables that can store words and letters in them. We use them to format basic strings, compare text such as passwords and usernames. You may be a bit confused on why you cannot just go ahead and do

pawn Code:
new string;
format(string, sizeof(string), "Hello");
//or
new string = "Hello";

We cant do either of those because a string requires multiple cells. Multiple cells means that it has to have a number after the brackets at least.

pawn Code:
new string[6] = "Hello";

You may be wondering why we have to do this. It's pretty simple really, it's because each letter of the word requires a separate cell. The letters themselves are not actually stored as letters in the variable either. You can test this by trying this

pawn Code:
new string[6] = "Hello";
printf("%d", string[0]);

You would think that it would print 'H' but it doesn't. It would only print 'H' if we used '%s' because %s converts the variable number in the cell to a string. Although, %s will make the print the whole string from the cell we chose onwards rather then the single cell

pawn Code:
new string[6] = "Hello";
printf("%s", string[0]);

The above code will print Hello rather then printing 'H'. To print just the single letter 'H' we would have to create a whole new string with just the single letter in it or you could use %c instead.

Why cant I just compare it using '=='?
This is kinda answered above really. '==' is used to compare single cells, whereas strings are multi celled. You can however compare every single letter to each other. This is comparing the numbers which are actually letters in the single cells so

pawn Code:
new string1[6] = "";
new string2[5] = "hell";
if(string1[0] == string2[0])
{
//Do something
}

The code above is checking if h is equal to h. This is the same as checking if 104 == 104 because the value of h that is stored for a string is 104 because it stores a number rather then a letter in the cell. So pretty much, you kinda get the reason why we cannot check if strings equal each other because they have more than one number to compare. We could either use strcmp to compare a string or check if each cell of the strings equal each other. The below example would be correct but only if the case was the same as each character has a different value.

pawn Code:
new string1[6] = "Hello";
new string2[6] = "Hello";
for (new id; id<6; id++)
{
if(string1[id] != string2[id]) return false; //If the strings don't equal (They do though).
}
return true;


What are single quotes used for in strings ( ' and ' )?
Single quotes are used for pretty much changing a letter into a number. There is a few uses for this such as checking if a single letter at a certain place in a string.

pawn Code:
public OnPlayerText(playerid, text[])
{
if(text[0] /*Text[0] is a single cell of the string text */ == '@')
{
//Do something
}
return 1;
}

This code above will change the '@' into a number which is 64. Now that we have two numbers, we can compare them using '==' and get a result.


Checking if a string is empty
There is a few methods that allow us to check if strings are empty or not. Our first one is
pawn Code:
if(!string[0])
This checks if there is anything in the first cell of the string. If there isn't anything in the first cell of the string, there is most likely not going to be anything in the rest of the strings either. We are checking if the first cell is 0 and as there is no character that fills the position of 0 (except null).

We could also use EOS to check if there is nothing in there. EOS is pretty much just representing "\0" which also represents 0.

pawn Code:
if(string[0] == EOS)
//Is actually if(string[0] == '\0')

Conclusion
This was a pretty rushed tutorial. I tried to cover the subject but not in too much depth so that the readers would get lost half way through it. I hope that you kind of understand how strings work now . Any criticism is welcome because I probably got half of it wrong .