AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [HELP]edit content from string (strbreak) (https://forums.alliedmods.net/showthread.php?t=221342)

seriousspot 07-20-2013 19:28

[HELP]edit content from string (strbreak)
 
for example i got queried sql result from row data:

Quote:

2440 0 2 5 3 0 0 0 0 0 0 0 0
how do i edit only 2440 and set as variable(i can set value what ever i want to, this supposed to be only numbers) without touching other values

lets say i have successfully edited 2440 to 3444, and whole string should look like:

Quote:

3444 0 2 5 3 0 0 0 0 0 0 0 0
(with spaces as original)

other contents rest same

Black Rose 07-21-2013 17:27

Re: [HELP]edit content from string (strbreak)
 
You could break it by spaces with strbreak(), turn the string into a number with str_to_num(), edit the number and then add the two parts together again using formatex(%d %s, first_number, rest_of_string)

This example reproduces your example:
Code:
    new left[32], right[128], test[128] = "2440 0 2 5 3 0 0 0 0 0 0 0 0";         strbreak(test, left, 31, right, 127);         new leftnum = str_to_num(left);     leftnum += 1004;         formatex(test, 127, "%d %s", leftnum, right);         server_print("%s", test);
Output:
Code:

3444 0 2 5 3 0 0 0 0 0 0 0 0
If you want to break it further you could use strbreak() or even parse() if you want to break most of it apart.

seriousspot 07-23-2013 15:08

Re: [HELP]edit content from string (strbreak)
 
Quote:

Originally Posted by Black Rose (Post 1995946)
You could break it by spaces with strbreak(), turn the string into a number with str_to_num(), edit the number and then add the two parts together again using formatex(%d %s, first_number, rest_of_string)

This example reproduces your example:
Code:
    new left[32], right[128], test[128] = "2440 0 2 5 3 0 0 0 0 0 0 0 0";         strbreak(test, left, 31, right, 127);         new leftnum = str_to_num(left);     leftnum += 1004;         formatex(test, 127, "%d %s", leftnum, right);         server_print("%s", test);
Output:
Code:

3444 0 2 5 3 0 0 0 0 0 0 0 0
If you want to break it further you could use strbreak() or even parse() if you want to break most of it apart.


Thank you, thats exact what i need, but found it earlier, so thanks for lightweight code, ill improve mine with this :)


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

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