2013/4/15整理:objective-c
將整數轉換成字符串 Convert Integer to NSString:
方法一:
int Value = 112233;
NSString *ValueString = [NSString stringWithFormat:@"%d", Value];
方法二:
[[NSNumber numberWithInt: 123] stringValue];
獲得C風格的字符串 C String
char *ValueasCString = (char *)[ValueString UTF8String];
將字符串轉換成整數或浮點數
Convert String to Integer or float
NSString has some useful methods:
-(int) intValue
-(float) floatValue
NSString *aNumberString = @"123";
int i = [aNumberString intValue];
字符串轉換爲日期
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//實例化一個NSDateFormatter對象
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//設定時間格式,這裏能夠設置成本身須要的格式
NSDate *date =[dateFormat dateFromString:@"1980-01-01 00:00:01"];
日期轉換爲字符串
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//實例化一個NSDateFormatter對象
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//設定時間格式,這裏能夠設置成本身須要的格式
NSString *currentDateStr = [dateFormat stringFromDate:[NSDate date]];
使用NSNumberFormatter將數值轉換成字符串
Simplifying NSNumberFormatter Use on the iPhone With Objective-C Categories
Here are the different formatter styles, and the result when converting [NSNumber numberWithDouble:123.4] into an NSString.
NSNumberFormatterNoStyle (123)
NSNumberFormatterDecimalStyle (123.4)
NSNumberFormatterCurrencyStyle ($123.40)
NSNumberFormatterPercentStyle (12,340%)
NSNumberFormatterScientificStyle (1.234E2)
NSNumberFormatterSpellOutStyle (one hundred and twenty-three point four)
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
NSString *string = [formatter stringFromNumber:number];
[formatter release];