AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   shorten a string ? (https://forums.alliedmods.net/showthread.php?t=223311)

Arkarr 08-12-2013 04:58

shorten a string ?
 
I want to retunr a part of my string. How I'm supposed to do that ?

Exemple :

new String:test[32];
Format(test, sizeof(test), "just a test");

And I want to shorten the content of test to "just"

Impact123 08-12-2013 07:11

Re: shorten a string ?
 
They easiest you can do in this case is this.
PHP Code:

test[4] = '\0'

Yours sincerely
Impact

Arkarr 08-12-2013 07:32

Re: shorten a string ?
 
Quote:

Originally Posted by Impact123 (Post 2011373)
They easiest you can do in this case is this.
PHP Code:

test[4] = '\0'

Yours sincerely
Impact

So, by doing this if I do something like this :

if(test[4] == "test")
PrintToChatAll("Sucess, the value of "test" is %s", test[0]);

it's will work ?

EDIT : Yep, it's work.

Impact123 08-12-2013 07:54

Re: shorten a string ?
 
No, don't do that.
Strings are char arrays, thus the number there is a index of the array which refers to a single char.
You can't compare chars with strings that way, use something like this instead.
Using an string index with PrintTo* is a special case i won't explain here, more info below.
Before you continue, please head over here and read how strings in SourcePawn work.

Yours sincerely
Impact

Arkarr 08-12-2013 08:39

Re: shorten a string ?
 
Quote:

Originally Posted by Impact123 (Post 2011406)
No, don't do that.
Strings are char arrays, thus the number there is a index of the array which refers to a single char.
You can't compare chars with strings that way, use something like this instead.
Using an string index with PrintTo* is a special case i won't explain here, more info below.
Before you continue, please head over here and read how strings in SourcePawn work.

Yours sincerely
Impact

Herm, okay thanks. Anyway, Java is more easier :).

Impact123 08-12-2013 08:46

Re: shorten a string ?
 
You don't really need to fullquote.

Yours sincerely
Impact

Arkarr 08-12-2013 09:03

Re: shorten a string ?
 
Okay, but now I don't understand. How I can use this ?

PHP Code:

if(StrEqual(test"just"))
{
       
PrintToChat("Work");
}
else
{
       
PrintToChat("Don't work.");



It does print : "Don't work," :(

Marcus_Brown001 08-12-2013 09:21

Re: shorten a string ?
 
Do you know what the string is actually equivalent to? I am going to presume that it is as you stated earlier: "just a test". The native StrEqual is going to check if two strings are 100% equal (use the bool to enable/disable case sensitivity).

So if you define String test like this:
PHP Code:

new String:sTest[] = { "just a test" }; 

and then check this:
PHP Code:

if (StrEqual(sTest"just"))
{
    
LogError("This Worked");
} else
{
    
LogError("This Did Not Work");


Then it is ALWAYS going to return false, because String:sTest is not "just" but rather "just a test". For that kind of a check you want to do something like this:
PHP Code:

if (StrContains(sTest"just") != -1)
{
    
LogError("This Worked");
} else
{
    
LogError("This Did Not Work");


StrContains is a nice native that simply checks if one string is found inside another; if the string is not found, a value of -1 is returned (this is why you specify != -1). Hope this helps you!

asherkin 08-12-2013 09:24

Re: shorten a string ?
 
Quote:

Originally Posted by Impact123 (Post 2011406)
Using an string index with PrintTo* is a special case i won't explain here, more info below.

No, it's for all natives.

Impact123 08-12-2013 10:11

Re: shorten a string ?
 
He used PrintToChat in his code, thus i referred to that.

Yours sincerely
Impact


All times are GMT -4. The time now is 02:05.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.