字符串和时间(NSDate)的常用方法

// 创建字符串

- (void)CreatString

{

// <1> 创建字符创

NSString *strS = @"This is a String!";

// <2> 创建空字符串,给予赋值

NSString *strK = [[NSString alloc] init];

strK = @"This is a String!";

NSLog(@"strS:%@, strK%@",strS,strK);

// <3> 使用变量初始化字符串

NSString *name = @"aofe";

NSString *strN = [NSString stringWithFormat:@"My name is %@",name];

NSLog(@"strN:%@",strN);

}

// 判断是否包含某字符串

- (void)IsContainsString

{

NSString *str1 = @"NSStringInformation.txt";

// <1> 检查字符串是否以另一个字符串开头

[str1 hasPrefix:@"NSString"] == 1? NSLog(@"YES") :NSLog(@"NO");

// <2> 检查字符串是否以另一个字符串结尾

[str1 hasSuffix:@".txt"] == 1? NSLog(@"YES") :NSLog(@"NO");

// <3> 检查字符串是否包含其他字符串

[str1 rangeOfString:@"Information"].length > 0 ? NSLog(@"YES") :NSLog(@"NO");

}

// 读写字符串

-(void)WriteAndReadString

{

// 文件路径

NSString *path = @"wenJianQuanLuJing";

// <1> 从文件读取字符串

NSString *strW = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

// <2> 将字符串写入到文件

[strW writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];

}

// 比较两个字符串

-(void)ComparerTwoString

{

NSString *str1 = @"This is String1";

NSString *str2 = @"THIS is String2";

// 比较两个字符串是否相等

BOOL result1 = [str1 isEqualToString:str2];

// 比较两个字符串(comparer方法返回三种值:NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending)

NSComparisonResult result2 = [str1 compare:str2];

// 不考虑大小比较字符串

NSComparisonResult result3 = [str1 caseInsensitiveCompare:str2];

NSLog(@"result1:%d,result2:%ld,result3:%ld",result1,(long)result2,(long)result3);

}

// 改变字符串的大小写

-(void)ChangeStringCase

{

NSString *str1 = @"this is string1";

NSString *str2 = @"THIS IS STRING2";

// 全部大写

[str1 uppercaseString];

// 全部小写

[str2 lowercaseString];

// 首字母大写

[str1 capitalizedString];

}

// 在字符串中搜索子串

-(void)SearchString

{

NSString *str1 = @"This is String1";

NSString *str2 = @"is";

NSRange range = [str1 rangeOfString:str2];

NSLog(@"location:%lu,length:%lu",(unsigned long)range.location,(unsigned long)

range.length);

}

// 替换字符串

-(void)ReplaceString

{

NSString *strL = @"hello china";

NSString *strN = [strL stringByReplacingOccurrencesOfString:@"china" withString:@"beijing"];

NSLog(@"strL:%@,strN:%@",strL,strN);

}

// 分隔字符串成数组

-(void)ComponentsString

{

NSString *str = @"a b c d e";

// 以空格分隔字符串成数组

NSArray *arr = [str componentsSeparatedByString:@" "];

NSLog(@"arr:%@,arr.count:%lu",arr,(unsigned long)arr.count);

}

// 数组拼接成字符串

-(void)ArrayToString

{

NSArray *array = [NSArray arrayWithObjects:@"this",@"is",@"String", nil];

// 用空格隔开数组中的元素

NSString *str = [array componentsJoinedByString:@" "];

NSLog(@"str:%@",str);

}

// 从字符串中抽取出新的字符串

-(void)DrawNewString

{

NSString *str = @"This is String";

// 从字符串的开头一直截取到指定位置,但不包括该位置的字符.

NSString *str1 = [str substringToIndex:5];

// 从指定位置开始(包括自定位置的字符串)一直到最后

NSString *str2 = [str substringFromIndex:5];

// 按照所给定的位置和长度,任意的从字符串中截取新的字符串

NSString *str3 = [str substringWithRange:NSMakeRange(4, 5)];

NSLog(@"str1:%@,str2:%@,str3:%@",str1,str2,str3);

}

// 可变字符串的操作

- (void)NSMutableStringOperation

{

// 给字符串分配容量

NSMutableString *strM = [NSMutableString stringWithCapacity:100];

NSLog(@"strM:%@",strM);

// 在已有的字符串后面添加字符串

NSMutableString *strM1 = [[NSMutableString alloc] initWithString:@"This is a"];

NSLog(@"strM1:%@",strM1);

[strM1 appendString:@"NSMutableString"];

NSLog(@"strM1:%@",strM1);

// 在已有字符串中按照所给出的范围和长度删除字符

[strM1 deleteCharactersInRange:NSMakeRange(0, 5)];

NSLog(@"strM1:%@",strM1);

// 在字符串指定位置插入字符串

[strM1 insertString:@"Hello" atIndex:0];

NSLog(@"strM1:%@",strM1);

// 将已有字符串替换成其他字符串

[strM1 setString:@"Hello World"];

NSLog(@"strM1:%@",strM1);

// 按照所给出的范围,用新字符串替换原来的字符串

[strM1 replaceCharactersInRange:NSMakeRange(0, 5) withString:@"Hi"];

NSLog(@"strM1:%@",strM1);

}

// 时间操作

- (void)NSDateOperation

{

// 得到当前日期

NSDate *date1 = [NSDate date];

NSLog(@"date:%@",date1);

NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:3];

// 比较日期:

// <1> 比较日期是否相同

BOOL result =[date1 isEqualToDate:date2];

// <2> 比较日期 返回时间较早的日期

NSDate *dateE = [date1 earlierDate:date2];

// <3> 比较日期 返回时间较晚的日期

NSDate *dateL = [date1 laterDate:date2];

NSLog(@"result:%d, dateE:%@, dateL:%@",result,dateE,dateL);

// 设置日期的格式

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

// 设置几种默认的显示效果

[formatter setTimeStyle:NSDateFormatterMediumStyle];

//自定义日期显示效果

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *time = [formatter stringFromDate:date1];

NSLog(@"%@",time);

}

时间: 2025-01-07 11:21:45

字符串和时间(NSDate)的常用方法的相关文章

字符串转成NSDate类型,计算与当前时间的相差,年数,天数,时分秒

NSString *dateStr=@"2013-08-13 20:28:40";//传入时间 //将传入时间转化成需要的格式 NSDateFormatter *format=[[NSDateFormatter alloc] init]; [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *fromdate=[format dateFromString:dateStr]; NSTimeZone *fromzo

python 下字符串格式时间比较

举例: 1 #-*-coding=utf-8-*- 2 __author__='zhongtang' 3 4 5 import time 6 strtime1='20160518010101' 7 strtime2='20160518020101' 8 9 #字符串变成时间数据结构 10 localtime1=time.strptime(strtime1,'%Y%m%d%H%M%S') 11 localtime2=time.strptime(strtime2,'%Y%m%d%H%M%S') 12

php将标准字符串格式时间转换成unix时间戳_strtotime

php 将标准字符串格式时间转换成unix时间戳的函数为:strtotime函数(PHP 4, PHP 5). strtotime函数详细参考: strtotime - 将任何英文文本的日期时间描述解析为 Unix 时间戳. 函数格式说明: int strtotime ( string $time [, int $now ] ) 本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 n

浅谈:字符串、时间格式的转换

字符串与时间格式的转换 -----常用的方法:1.拼接字符串的格式[String类型的一些常用的方法]: 2.simpledateformat格式 3.Date格式 1.SimpleDateFormat的用法: 1.1常用的方法: format(Date);将给定Date格式化为日期/时间字符串,并将结果添加到给定的StringBuffer parse(String);解析字符串文本,生成Date类型 2.Date的用法: 3.String的用法: 3.输出当前的时间: Date d = new

时间格式的转换(HTTP-GMT字符串--&gt;Long时间)

上一篇中,提供的帮助方法是将long类型的时间格式转化为GMT格式的时间格式, 本篇实现将GMT格式的数据转化为long类型的时间格式,具体的代码如下: //HTTP-GMT字符串-->Long时间public static long gmt2long(String gmttime){ try{ SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);  sd

时间格式化 字符串转时间(转)

/** 字符串转时间*/  function strToDate(str) { str = str.replace(/-/g,"/");var date = new Date(str ); } Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+"

JQuery 字符串转时间格式

//字符串转时间格式 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/, function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')'); return date; }

postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式

数据库中:字符串 转换为 时间格式 二者区别: to_data 转换为 普通的时间格式        to_timestamp 转换可为 时间戳格式出错场景: 比较同一天 日期大小的时候,很容易出错 例如:        select current_timestamp from pub_employee        结果如下:            select current_timestamp <= to_date('2018-03-12 18:47:35','yyyy-MM-dd hh

iOS开发时间戳与时间NSDate,时区的转换,汉字与UTF8,16进制的转换

http://blog.sina.com.cn/s/blog_68661bd80101njdo.html 标签: ios时间戳 ios开发时间戳 ios16进制转中文 ios开发utf8转中文 ios汉字转utf8和16进   //获取当前系统的时间戳+(long)getTimeSp{    long time;    NSDate *fromdate=[NSDate date];    time=(long)[fromdate timeIntervalSince1970];    return