Properly get string
#1

Of what I've read, you make a string by making an array.

Let's say

Код:
new newString[128];
Then you store strings in this. Let's say

Код:
newString = "Hello everybody";
But how do you GET this string then?
newString[0] will get the first letter, but how do you get the whole "Hello everybody"

Am I doing strings wrong, or am I just a noob? :P

-Thanks in forwards
Reply
#2

Just "newString". No index necessary.
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
Just "newString". No index necessary.
Thanks for replying. The problem is, that gives me the error 033: array must be indexed.
Reply
#4

Hello,

Here's a simple example to show you how it works.

Код:
// We create a string and put "Hello" inside.
// Note: We need 6 cells because there is an end-of-string caracter at the end ("\0").
new sText[6] = "Hello";

// Now we can show the string using functions:
printf("%s buddy", sText);

// It will render as: "Hello buddy"
Hope I helped. If you still have problems after that, please show us the portion of code you're trying to run.
Reply
#5

Quote:
Originally Posted by ArchB42
Посмотреть сообщение
Hope I helped. If you still have problems after that, please show us the portion of code you're trying to run.
Hello Thanks, I already read that one. So as it didn't really help me, here's an example:

I'm creating this:
OnPlayerChangeAnim(playerid, newAnim, oldAnim)


Whenever I want to compare newAnim to an anim, I do this:
Код:
new baseballBat[32];
baseballBat = "BASEBALL_BAT_1";

if(strcmp(baseballBat == newAnim)
{}
Then it tells me that baseballBat in the strcmp should be indexed. When I do baseballBat[0], I get no errors, because it of course is indexed then.
So how do I strcmp the whole String like this? Should I format baseballBat?
Reply
#6

Hold on a second. That is not the correct strcmp syntax. Do it like this:
pawn Код:
if(!strcmp(baseballBat, newAnim, true)) //true means that we don't care about the capital letters (for example, test is identical to TeSt)
{
//they are identical
}
else
{
//they are not identical, at least one character is different
}
This is the wiki page: https://sampwiki.blast.hk/wiki/Strcmp
Reply
#7

Quote:
Originally Posted by HazardouS
Посмотреть сообщение
Hold on a second. That is not the correct strcmp syntax. Do it like this:
pawn Код:
if(!strcmp(baseballBat, newAnim, true)) //true means that we don't care about the capital letters (for example, test is identical to TeSt)
{
//they are identical
}
else
{
//they are not identical, at least one character is different
}
This is the wiki page: https://sampwiki.blast.hk/wiki/Strcmp
!strcmp would be != and I want ==, therefore strcmp, right?..

Also true is optional, and I choose that it should care for capital letters
Reply
#8

You don't use == or != in strcmp, the code he provided will check if baseballBat and newAnim matches correctly, but yeah you can change the "true" to "false" if it should care for capital letters.
Reply
#9

Quote:
Originally Posted by CalvinC
Посмотреть сообщение
You don't use == or != in strcmp, the code he provided will check if baseballBat and newAnim matches correctly, but yeah you can change the "true" to "false" if it should care for capital letters.
I mean the ! in front of strcmp would check if they DIDN'T match?
Reply
#10

In an if-statement, !variable means "variable == 0". Strcmp returns 0 when the strings match, so !strcmp means match. When you say if(variable), it actually means if(variable != 0), so "if( strcmp(string1, string2) )" is true when string1 and string2 do NOT match.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)