Extracting string to different variables - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Extracting string to different variables (
/showthread.php?tid=651528)
Extracting string to different variables -
Kampott - 21.03.2018
So, basically i've got a variable date that stores time in "dd/mm/yyyy hh:mm
s" format, and i need to extract each part in it's own variable (dd goes to day, mm goes to month, yy goes to year etc..)
So, how can i do it?
Re: Extracting string to different variables -
Dayrion - 21.03.2018
Using str* functions (strmid, strdel, strfind, ...).
Re: Extracting string to different variables -
AdamsLT - 21.03.2018
You can use sscanf with optional delimiters.
PHP код:
new date[16] = "10/15/2018 22:35",
day, month, year, hour, minuter;
sscanf(date, "P< :/>iiiii", day, month, year, hour, minute);
This will scan the string for the optional delimiters : space, colon, forward slash
Not sure if this is fully correct but a variation of this should work.
https://sampforum.blast.hk/showthread.php?tid=570927
Re: Extracting string to different variables -
Maxandmov - 22.03.2018
As for str... functions, you might want to do it as following:
Relocate the data in buffer variable, detect the slash, copy everything you had there in separate variable, delete the copied item with a slash from the variable, and so on with checking proper chars (be it / or : ). Must be enough, but it might not be as beautiful as sscanf variation of approach.
Re: Extracting string to different variables -
Dayrion - 22.03.2018
Quote:
Originally Posted by Maxandmov
As for str... functions, you might want to do it as following:
Relocate the data in buffer variable, detect the slash, copy everything you had there in separate variable, delete the copied item with a slash from the variable, and so on with checking proper chars (be it / or : ). Must be enough, but it might not be as beautiful as sscanf variation of approach.
|
He must use sscanf as AdamsLT said ; it's better and faster. I didn't thought about it in first time.