Object-c总结之NSString
参照《iphone SDK 3开发快速上⼿》编写,特此声明
//创建⼀个NSString
NSString *myString1 = @"some string";
NSLog(@"%@",myString1);
//格式化创建
NSString *myString2 = [NSString stringWithFormat:@"%@",myString1];
NSLog(@"%@",myString2);
//返回数字的值
NSString *myString3 = @"1234abc";
double doubleString = [myString3 floatValue];//获得123
NSLog(@"%f",doubleString);
int intString = [myString3 intValue];//获得123
NSLog(@"%d",intString);
NSUInteger stringLenth = [myString3 length];
/*NSUinteger 的输出⽅式为%lu*/
NSLog(@"%lu",stringLenth);
//⽐较两个字符串,若相同返回TURE
BOOL areEqual =
[myString1 isEqualToString:myString2];
NSLog(@"areEqual = %d",areEqual);
//区分⼤⼩写的⽐较
BOOL areEqual1 = ([myString1 caseInsensitiveCompare:myString2] == NSOrderedSame);
NSLog(@"areEqual1 = %d",areEqual1);
//转换⼤⼩写
NSString *myString4 = @"asdfg";
NSString *upper = [myString4 uppercaseString];
NSString *lower = [upper lowercaseString];
NSLog(@"upper = %@",upper);
NSLog(@"lower = %@",lower);
//截去字符串(以空格为例)
NSString *myString5 = @"  one    two three  ";
NSString *trimmed1 = [myString5 stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndN
ewlineCharacterSet]];        //whitespaceAndNewlineCharacterSet去除前后的空格和换⾏符
NSString *trimmed2 = [myString5 stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
//whitespaceCharacterSet 去除前后的空格,实际效果来看只实现了去除⾸字母前⾯的空格
NSLog(@"myString5 = %@",myString5);
NSLog(@"trimmed1 = %@",trimmed1);
NSLog(@"trimmed2 = %@",trimmed2);
//predicate 断⾔断定,使基于,下⾯的⽅法实现删除空格
NSPredicate *noE = [NSPredicatepredicateWithFormat:@"SELF!=''"];
NSArray *part = [myString5 componentsSeparatedByCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];        NSArray *file = [part filteredArrayUsingPredicate:noE];
NSString *trimmed3 = [file componentsJoinedByString:@""];
NSLog(@"trimmed3 = %@",trimmed3);
//有已知字符串创建⼦字符串
NSString * number = @"abx cdefghi gklmn";
//substringToIndex获取字符串的前三个
//substringFromIndex从第三个开始截取到最后
NSString *myString6 = [number substringToIndex:3];
NSLog(@"myString6 = %@",myString6);
NSRange range = NSMakeRange(4, 5);
//⽤边界截取起始位置为第四个,长度为5个长度
NSString *aString = [number substringWithRange:range];
NSLog(@"aString = %@",aString);
/
/创建成数组
NSArray *arr = [number componentsSeparatedByString:@" "];
NSLog(@"arr = %@",arr);
//替换⼦字符串
NSString *myString7 = [number stringByReplacingOccurrencesOfString:@"efg"withString:@"aaa"];
NSLog(@"myString7 = %@",myString7);
//查⼦字符串,这段代码返回(4,3);
NSRange found = [number rangeOfString:@"cde"];
NSLog(@"found.location = %lu",found.location);
NSLog(@"%lu",found.length);
//判读那是否包含
BOOL found1 = ([number rangeOfString:@"cde"].location !=NSNotFound);
NSLog(@"found1 = %lu",found1);
//组合字符串
NSString *myString9 = [myString1 stringByAppendingString:myString7];
NSLog(@"myString9 = %@,myString1 = %@,myString7 = %@",myString9,myString1,myString7);
NSString *myString10 = [myString1 stringByAppendingFormat:myString7];
NSLog(@"myString10 = %@,myString1 = %@,myString7 = %@",myString10,myString1,myString7);
//将⽂件内容写⼊到字符串中
NSString *fileContents = [NSString stringWithContentsOfFile:@""];
//获取⽂件扩展名
字符串常量是什么样的
NSString *filename = @"";
NSString *fileExtension = [filename pathExtension];
NSLog(@"fileExtension = %@",fileExtension);
//将URL内容写⼊字符串,有点问题
NSLog(@"url = %@",url);
NSString *pageContents  = [NSString stringWithContentsOfURL:url];
//NSLog(@"pageContents = %@",pageContents);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。