SA-MP Forums Archive
Appending into string - 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: Appending into string (/showthread.php?tid=509024)



Appending into string - Tayab - 25.04.2014

Hi, I've got a string that I wanna edit, is there any function for that?

Lets say I've got a string '25 Apr 2014' which I wanna edit to '25 Apr, 2014' (adding comma after Apr). Can someone help me out?


Re: Appending into string - Conradus - 25.04.2014

I think you can use sscanf for this (untested):
pawn Код:
new DateString[] = "25 Apr 2014";
new Day, Month[3], Year;
if(sscanf(DateString, "dsd", Day, Month, Year)) {
    print("String is not in the right format!");
}
else {
    new NewDateString[12];
    format(NewDateString, sizeof(NewDateString), "%d %s, %d", Day, Month, Year);
    print(NewDateString); // should print: 25 Apr, 2014
}



Re: Appending into string - Tayab - 25.04.2014

I've got a string which is already been made, I don't have data in pieces like day, month and year.


Re: Appending into string - Conradus - 25.04.2014

Yes I know, you just split the string up into a day, month and year using sscanf, after that, you can rebuild the string using "format" and add anything you want, like the comma after "Apr"


Re: Appending into string - Vince - 25.04.2014

What is the source of this string, if I may ask? Where is it generated?


Re: Appending into string - Tayab - 25.04.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
What is the source of this string, if I may ask? Where is it generated?
I'm actually using the getdate function but I can't put the comma in the format because I've to put the data in database and if I add the comma in it, it just mingles everything up.

pawn Код:
getdate(Year,Month,Day);
format(date,sizeof(date),"%02d %s %d",Day,MonthToStringMonth(Month),Year);



Re: Appending into string - Conradus - 25.04.2014

Have you tried my solution?
pawn Код:
stock AddCommaAfterMonth(DateString[]) {
    new Day, Month[3], Year;
    if(sscanf(DateString, "dsd", Day, Month, Year)) {
        print("Comma could not be added to the Date String (Reason: string not in the right format)");
    }
    else {
        format(DateString, sizeof(DateString), "%d %s, %d", Day, Month, Year);
    }
    return DateString;
}
If we assume your date string is in the variable DateString, you can now just add the comma like this:
pawn Код:
DateString = AddCommaAfterMonth(DateString);