AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [HELP] How to control only first few characters of a string (https://forums.alliedmods.net/showthread.php?t=277619)

Ardonicek 01-12-2016 13:25

[HELP] How to control only first few characters of a string
 
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

addons_zz 01-12-2016 13:32

Re: [HELP] How to control only first few characters of a string
 
native equali(const a[],const b[],c=0);

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

Ardonicek 01-12-2016 13:57

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

Originally Posted by addons_zz (Post 2382588)
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 ].

addons_zz 01-12-2016 14:19

Re: [HELP] How to control only first few characters of a string
 
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

HamletEagle 01-12-2016 14:23

Re: [HELP] How to control only first few characters of a string
 
Just use equal/equali and specify the number of characters that you want to be checked in the 3 rd param.

Ardonicek 01-12-2016 15:13

Re: [HELP] How to control only first few characters of a string
 
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?

addons_zz 01-12-2016 15:18

Re: [HELP] How to control only first few characters of a string
 
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" );

siriusmd99 01-12-2016 15:21

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

Originally Posted by addons_zz (Post 2382641)
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.

Ardonicek 01-12-2016 15:48

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

Originally Posted by addons_zz (Post 2382641)
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 :P


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

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