Appending into string
#1

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?
Reply
#2

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
}
Reply
#3

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

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"
Reply
#5

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

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);
Reply
#7

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);
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)