Raised This Month: $ Target: $400
 0% 

[HELP] How to control only first few characters of a string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ardonicek
Senior Member
Join Date: Feb 2013
Location: My home
Old 01-12-2016 , 13:25   [HELP] How to control only first few characters of a string
Reply With Quote #1

Hello, I need a little help.

For example if the string I have would be "123456789".

If the string begins with "123", it would do the function I want to.
If it begins with any other number/character, I want to perform another function (if/else).

That's all, thank you!

simply:
string is 123456789
if string begins with 123 = perform function
else = perform another function
__________________
Latest plugin: dHUD Round | Timeleft
Ardonicek is offline
Send a message via ICQ to Ardonicek Send a message via Skype™ to Ardonicek
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 01-12-2016 , 13:32   Re: [HELP] How to control only first few characters of a string
Reply With Quote #2

native equali(const a[],const b[],c=0);

Example:
Code:
    if( lenMapName > 4         && equali( mapName[ lenMapName - 4 ], ".bsp", 4 ) )     {         mapName[ lenMapName - 4 ] = '^0';
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective
addons_zz is offline
Ardonicek
Senior Member
Join Date: Feb 2013
Location: My home
Old 01-12-2016 , 13:57   Re: [HELP] How to control only first few characters of a string
Reply With Quote #3

Quote:
Originally Posted by addons_zz View Post
native equali(const a[],const b[],c=0);

Example:
Code:
    if( lenMapName > 4         && equali( mapName[ lenMapName - 4 ], ".bsp", 4 ) )     {         mapName[ lenMapName - 4 ] = '^0';

Can you explain this part of code?
Code:
        mapName[ lenMapName - 4 ] = '^0';

If I understand it well, the first part of code means - if lenMapName variable has length of more than 4 characters & first 4 characters of mapName variable match and it ends with bsp, then it does the function?
But I don't understand the thing i quoted in [ PAWN ].
__________________
Latest plugin: dHUD Round | Timeleft
Ardonicek is offline
Send a message via ICQ to Ardonicek Send a message via Skype™ to Ardonicek
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 01-12-2016 , 14:19   Re: [HELP] How to control only first few characters of a string
Reply With Quote #4

If the last 4 mapName characters are equal to '.bsp', then they are removed from the string.

'^0' is espace character for the integer 0. It it used to not use an integer, and let the code reader know that mapName stores an string instead of a array on integers.

See also:
  1. https://forums.alliedmods.net/showthread.php?p=2376884#post2376884
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 01-12-2016 at 14:19. Reason: spelling fix
addons_zz is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-12-2016 , 14:23   Re: [HELP] How to control only first few characters of a string
Reply With Quote #5

Just use equal/equali and specify the number of characters that you want to be checked in the 3 rd param.
__________________
HamletEagle is offline
Ardonicek
Senior Member
Join Date: Feb 2013
Location: My home
Old 01-12-2016 , 15:13   Re: [HELP] How to control only first few characters of a string
Reply With Quote #6

Well, what if I want to use it for a cvar?
for example - amx_randomcvar has a value: 123456
I just need to check for the first three characters, if they are equal, then the function I want (for example user_kill) may be triggered.

Can you post me an example of that?
__________________
Latest plugin: dHUD Round | Timeleft
Ardonicek is offline
Send a message via ICQ to Ardonicek Send a message via Skype™ to Ardonicek
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 01-12-2016 , 15:18   Re: [HELP] How to control only first few characters of a string
Reply With Quote #7

Code:
    get_pcvar_string( cvar_string_pointer, mapFilename, charsmax( mapFilename ) );       if( strlen( arg1 ) > 2         && equali( mapFilename, "123", 3 ) )     {         ...     }

Prefer 'get_pcvar_string' instead of 'get_cvar_string' because they are faster, but you need the cvar pointer, which you can save at register_cvar. Example:
Code:
    cvar_string_pointer = register_cvar( "amx_supercool", "cooler" );
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 01-12-2016 at 15:30. Reason: spelling fix
addons_zz is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 01-12-2016 , 15:21   Re: [HELP] How to control only first few characters of a string
Reply With Quote #8

Quote:
Originally Posted by addons_zz View Post
Code:
    get_pcvar_string( cvar_string_pointer, mapFilename, charsmax( mapFilename ) );       if( strlen( arg1 ) > 2         && equali( mapFilename, "123", 3 ) )     {         ...     }
Here is more efficient to use equal instead of equali because you don't have letters.
siriusmd99 is offline
Ardonicek
Senior Member
Join Date: Feb 2013
Location: My home
Old 01-12-2016 , 15:48   Re: [HELP] How to control only first few characters of a string
Reply With Quote #9

Quote:
Originally Posted by addons_zz View Post
Code:
    get_pcvar_string( cvar_string_pointer, mapFilename, charsmax( mapFilename ) );       if( strlen( arg1 ) > 2         && equali( mapFilename, "123", 3 ) )     {         ...     }

Prefer 'get_pcvar_string' instead of 'get_cvar_string' because they are faster, but you need the cvar pointer, which you can save at register_cvar. Example:
Code:
    cvar_string_pointer = register_cvar( "amx_supercool", "cooler" );

Works like a charm! Thank you!
Oh and sorry for my English
__________________
Latest plugin: dHUD Round | Timeleft
Ardonicek is offline
Send a message via ICQ to Ardonicek Send a message via Skype™ to Ardonicek
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 09:28.


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