[Tutorial] Understanding Strings - Short / Basic
#1

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 Код:
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 Код:
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 Код:
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 Код:
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 Код:
new string1[6] = "hello";
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 Код:
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 Код:
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 Код:
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 Код:
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 :P.
Reply
#2

Well done, Killa.

Very well explained
Reply
#3

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
Checking if a sting is empty
You're welcome.

Nice tutorial.
Reply
#4

Quote:
Originally Posted by MP2
Посмотреть сообщение
You're welcome.

Nice tutorial.
Haha, thanks. I didn't really fully spell check the whole thing :P.
Reply
#5

Just skimmed over it and noticed this

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
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.
for a single character you can use the character placeholder %c

pawn Код:
printf("d = %d c = %c s = %s", string[0], string[0], string[0]);
Reply
#6

Quote:
Originally Posted by cessil
Посмотреть сообщение
Just skimmed over it and noticed this



for a single character you can use the character placeholder %c

pawn Код:
printf("d = %d c = %c s = %s", string[0], string[0], string[0]);
Forgot about that, silly me :>.
Reply
#7

mint, nice tutorial thekiller
Reply
#8

That's a nice tutorial, pretty usefull for beginners.
Reply
#9

Quote:

Checking if a string is empty

What about strlen?

And maybe:
Quote:

for (new id; id<sizeof(string); id++)

And give an example for strcmp
Reply
#10

Nice tutorial, but why do you (for example) need to use 5 cells for a string like 'hell'?
Reply
#11

Quote:
Originally Posted by Hiddos
Посмотреть сообщение
Nice tutorial, but why do you (for example) need to use 5 cells for a string like 'hell'?
4 characters + Null character. Would you like me to add that to the tutorial? I did kind of explain the single cell for the single character but I'll add now.
Reply
#12

Nice tutorials, This helps alot. thanks.
Reply
#13

cccc
Reply
#14

You could also add equating indices of strings to the ASCII code which will result it into changing it to the corresponding character ! Good job, by the way!
Reply
#15

Good Tutorial Killa. Yeah strcmp , strmid , etc should be shown too.
Reply
#16

Well explained
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)