另外的一篇博文http://fei263.blog.163.com/blog/static/927937242009526112420548/
1。如何根据时间来生存一个字符串
NSDate *now = [NSDate date];
NSString *formatStr = [NSString stringWithFormat:@"%.0f", [now timeIntervalSince1970]];
我运行时返回“1317575895”
2。NSDateFormatter设定日期格式
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setAMSymbol:@"AM"];
[dateFormatter setPMSymbol:@"PM"];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mmaaa"];
NSDate *date = [NSDate date];
NSString * s = [dateFormatter stringFromDate:date];
显示效果为:03/10/2011 01:22
下面附上几个格式:这个是从Cocoachina上来的。呵呵。
yyyy:MM:dd G 'at' HH:mm:ss zzz 1996.07.10 AD at 15:08:56 PDT
EEE, MMM d, "yy Wed,july 10, '99
h:mm a 12:08 PM
hh 'o"clock' a,zzzz 12 o'clock PM, Pacific Daylight Time
K:mm a, z 0:00 PM, PST
yyyyy,MMMM.dd GGG hh:mm aaa 01996.july.10 AD 12:08 PM
下面是得到当前的年,月,日,时,分,秒。NSCalendar *cal = [NSCalendar currentCalendar];
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dd = [cal components:unitFlags fromDate:date];
int y = [dd year];
int m = [dd month];
int d = [dd day];
int h = [dd hour];
int m = [dd minute];
int s = [dd second];
3。获得临时文件夹
NSString* strPath = NSHomeDirectory();
NSDate *now = [NSDate date];
strPath = [strPath stringByAppendingPathComponent:@"tmp"];
输出:/var/mobile/Applications/0A0DE264-EFFB-4C90-B622-089E37E3FD20/tmp
4.如何产生随机数
// Get random number between 0 and 99
int x = arc4random() % 100;
// Get random number between 500 and 999
int y = (arc4random() % 501) + 500);
5。整数,浮点,字符串如何转换
NSString* s = [NSString stringWithFormat:@"%i", i];
NSString *string = …;
int value = [string intValue];
还可以通过floatValue或者doubleValue进行转换。
6.字符串操作
// 删除字符串头部,尾部的空格,并将字符串中间的两个空格替换为一个空格
//比如“ 我 是 中国 人 ”执行后变为"我 是 中国 人"
+(NSString *)repairBlankForTag:(NSString*)tag
{
while (1 == [tag hasPrefix:@" "])
{
tag = [tag substringFromIndex:1];
NSLog(@"************%@",tag);
}
while (1 == [tag hasSuffix:@" "])
{
tag = [tag substringToIndex:([tag length] - 1)];
NSLog(@"************%@",tag);
}
do {
tag = [tag stringByReplacingOccurrencesOfString:@" " withString:@" "];
NSLog(@"************%@",tag);
} while ([tag rangeOfString:@" "].length > 0);
return tag;
}