AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Parsing strings with variable length (https://forums.alliedmods.net/showthread.php?t=108403)

Mlk27 11-04-2009 22:54

Parsing strings with variable length
 
Strings:

Quote:

"This is random username 10000"
"Second username 500"
"Ludwig Van Beethoven 70"
How do I parse each of them into 2 strings? The bolded texts are usernames and the numbers at the end are their score.

Exolent[jNr] 11-04-2009 23:00

Re: Parsing strings with variable length
 
Code:
new szString[ 96 ]; // contains the username and number new szName[ 64 ], szNumber[ 32 ]; new iLen = strlen( szString ); for( new iPos = iLen - 1; iPos >= 0; iPos-- )     if( szString[ iPos ] == ' ' ) {         copy( szName, iPos, szString );         copy( szNumber, ( iLen - iPos - 1 ), szString[ iPos + 1 ] );         break;     } } new iNumber = str_to_num( szNumber )

Mlk27 11-04-2009 23:20

Re: Parsing strings with variable length
 
Are there other faster ways to do this as it will be used in client_authorized or that code is already fast enough?

fysiks 11-04-2009 23:41

Re: Parsing strings with variable length
 
Where are the strings coming from?

Bugsy 11-04-2009 23:53

Re: Parsing strings with variable length
 
Here's a way you can do it without using copy or the name & number string variables.
PHP Code:

new szString96 ] = "the user name 5000"
new 
iLen strlenszString ) , iPos;
    
for( 
iPos iLen 1iPos >= 0iPos-- )
{
    if( 
szStringiPos ] == ' ' 
    {
        
szStringiPos ] = EOS;
        break;
    }
}

server_print"^"%s^" ^"%s^"" szString szStringiPos ] ); 



All times are GMT -4. The time now is 17:39.

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