jsaonboo
jsaonboo

Reputation: 91

How to get the desired string format in ios

I have the text box with value 2016-01-12.I created the string like this

NSString * date = textbox.text;

Now the string having values 2016-01-12. I want to display like this.

"spent" : "2016-01-12"

Now i done

NSString * spent1 = @" \"spent\" : ";
NSString * new = [NSString stringWithFormat:@"%@%@",spent1,date];

Now i am getting like this:"spent":2016-01-12

But i need "spent":"2016-01-12".thanks for suggestion and help

Upvotes: 0

Views: 96

Answers (4)

Sudheer Kolasani
Sudheer Kolasani

Reputation: 283

Try this...

 NSString * date =@"2016-01-12";
    NSString * dateStr=[NSString stringWithFormat:@"\"%@\"",date];

    NSString * spent1 = @" \"spent\" : ";
    NSString * new = [NSString stringWithFormat:@"%@ %@",spent1,dateStr];
    NSLog(@"------>%@",new);

Upvotes: 0

Saheb Roy
Saheb Roy

Reputation: 5967

You can do this --

NSString * spent1 = [NSString stringWithFormat:@" \"spent\" : "/%@"/",date];

Or you can use Date formatter which is much more subtle as @Uma Madhavi suggested

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 1832

 NSString *str =  [NSString stringWithFormat:@"\"%@\" : \"%@\"", @"spent", textbox.text];

Upvotes: 1

Uma Madhavi
Uma Madhavi

Reputation: 4917

For getting date means use this format.

NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(@"date is %@",dateString);

Upvotes: 1

Related Questions