Raised This Month: $ Target: $400
 0% 

shorten a string ?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 08-12-2013 , 04:58   shorten a string ?
Reply With Quote #1

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"
__________________
Want to check my plugins ?
Arkarr is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 08-12-2013 , 07:11   Re: shorten a string ?
Reply With Quote #2

They easiest you can do in this case is this.
PHP Code:
test[4] = '\0'
Yours sincerely
Impact
__________________
Impact123 is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 08-12-2013 , 07:32   Re: shorten a string ?
Reply With Quote #3

Quote:
Originally Posted by Impact123 View Post
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.
__________________
Want to check my plugins ?

Last edited by Arkarr; 08-12-2013 at 07:37.
Arkarr is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 08-12-2013 , 07:54   Re: shorten a string ?
Reply With Quote #4

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
__________________

Last edited by Impact123; 08-12-2013 at 07:56.
Impact123 is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 08-12-2013 , 08:39   Re: shorten a string ?
Reply With Quote #5

Quote:
Originally Posted by Impact123 View Post
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 .
__________________
Want to check my plugins ?
Arkarr is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 08-12-2013 , 08:46   Re: shorten a string ?
Reply With Quote #6

You don't really need to fullquote.

Yours sincerely
Impact
__________________
Impact123 is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 08-12-2013 , 09:03   Re: shorten a string ?
Reply With Quote #7

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,"
__________________
Want to check my plugins ?

Last edited by Arkarr; 08-12-2013 at 09:05. Reason: fail with brakets.
Arkarr is offline
Marcus_Brown001
AlliedModders Donor
Join Date: Nov 2012
Location: Illinois, United States
Old 08-12-2013 , 09:21   Re: shorten a string ?
Reply With Quote #8

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!
Marcus_Brown001 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 08-12-2013 , 09:24   Re: shorten a string ?
Reply With Quote #9

Quote:
Originally Posted by Impact123 View Post
Using an string index with PrintTo* is a special case i won't explain here, more info below.
No, it's for all natives.
__________________
asherkin is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 08-12-2013 , 10:11   Re: shorten a string ?
Reply With Quote #10

He used PrintToChat in his code, thus i referred to that.

Yours sincerely
Impact
__________________

Last edited by Impact123; 08-12-2013 at 10:11.
Impact123 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:28.


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