// 字符初始化
void initStaticString(){
NSString *s1 = @"abc";//[NSString stringWithString:@"abc"];
NSString *ss1 = [[NSString alloc] initWithString:s1];
NSString *s2 = [NSString stringWithFormat:@"abcd"];
NSString *s3 = [NSString stringWithUTF8String:"abcde"];
NSString *s4 = [NSString stringWithCString:"abc123" encoding:NSUTF8StringEncoding];
NSString *s5 = [NSString stringWithContentsOfURL:
[NSURL URLWithString:@"www.baidu.com"]
encoding:NSUTF8StringEncoding error:nil];
}
// 字符串截取
void mysubString(){
NSString *str=@"abcdefg123456";
//计算字符串长度
unsigned long n = [str length];
NSLog(@"%ld",n);
//从第3个位置开始截取,索引从3开始
NSLog(@"%@",[str substringFromIndex:3]); //Output:defg123456
//截取到第3个位置,索引到3-1,不包括3
NSLog(@"%@",[str substringToIndex:3]); //Output:abc
//截取一段字符串,从2开始的5个字符串
NSRange range = NSMakeRange(2, 5); //声明结构体
NSLog(@"%@",[str substringWithRange:range]); //Output:cdefg
//
NSLog(@"%@",[str substringWithRange:NSMakeRange(2, 5)]); //Output:cdefg
NSString *str2=@"1,2,3,a,b,c";
//字符串分割
NSArray *array=[str2 componentsSeparatedByString:@","]; //Output:(1,2,3,a,b,c)
NSLog(@"%@",array);
NSRange rangeSub = [str rangeOfString:@"defg"];
//查找子串首字母在父串中的索引位置
NSLog(@"range.location=%ld",rangeSub.location); //Output:3
//查找子串在父串中占的长度,大于0表示找到了,等于0表示没找到
NSLog(@"range.length=%ld",rangeSub.length); //Output:4
}
//可变字符串 NSMutableString
void multalbeString(){
NSMutableString *mStr = [[NSMutableString alloc] initWithCapacity:0];
[mStr appendString:@"This is MutableString!"];
NSLog(@"%@",mStr); //Output:This is MutableString!
//在原字符串后面添加格式化字符串
[mStr appendFormat:@"My age is %d\n",23];
NSLog(@"%@",mStr); //Output:This is MutableString!My age is 23
//在原字符串指定位置开始插入字符串
[mStr insertString:@"AAA"atIndex: 8];
NSLog(@"%@",mStr); //Output:This is AAAMutableString!My age is 23
//删除指定范围内的一个子字符串
[mStr deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"%@",mStr); //Output:is AAAMutableString!My age is 23
//替换指定范围内的一个子字符串
[mStr replaceCharactersInRange:NSMakeRange(3, 3) withString:@"BBB"];
NSLog(@"%@",mStr); //Output:is BBBMutableString!My age is 23
}
void stringOther(){
NSMutableString *mStr = [[NSMutableString alloc] initWithCapacity:0];
[mStr appendString:@"abc abc abc WO WO WO!"];
//转换为大写,返回一个大写的新字符串
NSString* up = [mStr uppercaseString];
NSLog(@"up=%@",up);
NSLog(@"mstr=%@",mStr);
//转换为小写,返回一个小写的新字符串
NSString* low = [mStr lowercaseString];
NSLog(@"low=%@",low);
//返回一个首字母转换成大写的字符串
NSString* Abc = [mStr capitalizedString];
NSLog(@"Abc=%@",Abc);
}
//判断前缀后缀
void stringPreSuf(){
NSString* strURL = @"http://www.music.com/10/love.mp3";
BOOL isHasPrefix = [strURL hasPrefix:@"http://"];
BOOL hasSuffix = [strURL hasSuffix:@".mp3"];
NSLog(@"%d",isHasPrefix);
NSLog(@"%d",hasSuffix);
}
//字符串比较
void stringComare(){
NSString* s1 = @"abc";
NSString* ss1 = @"abc"; //此处ss1 s1 指向的是同一个地址
NSString* s2 = [[NSString alloc]initWithFormat:@"abc"];
if ([s1 isEqualToString:s2]) { //比较字符串的值
NSLog(@"OK!");
}
if (s1 == s2) { //比较字符串的地址,s1==s2 为假 s1==ss1 为真
NSLog(@"OK!");
}
int n = [s1 compare:s2]; //1,0,-1
NSLog(@"%d",n);
}
相关推荐
Objective-C里核心的处理字符串的类就是NSString和NSMutableString这两个类,这两个类完成了Objective-C中字符串大部分功能的处理。这两个类的最主要的区别是NSString创建赋值后不能动态修改长度和内容,除非给重新...
[map setObject:@"c" forKey:@"content"]; 或者 NSMutableArray *list = [[NSMutableArray alloc]init]; NSMutableDictionary *map1 = [[NSMutableDictionary alloc]init]; [map1 setObject:@"a1" forKey:@...
主要介绍了Objective-C中字符串NSString的常用操作方法总结,Objective-C中NSString和NSMutableString这两个类下包含了操作字符串的大多数方法,需要的朋友可以参考下
1 获取XML的数据DATA 2 调用解析类,提供对象名。 3 返回NSMutableDictionary或者NSMutableArray,键值封装。 示例见最后的注释。 XML封装类待续。。。 转载请注明来源,谢谢 ====== XmlResolve.h #...
Software engineering,programming language,operating system,iOS,OS,iPhone,iPad objective c,cocoa touch,SDK,object oriented design,Apple,Macintosh,tools,language,runtime,Xcode,objective-...
Objective-C中的类型推断 该项目包括几个宏和类类别,这些宏和类类别提供了有关编译器类型推断的提示。 从受Swift启发的简单let和var到替换for (in) {}循环的foreach () {}构造。 如果没有类型推断,则类型容易出错...
客观教学这是我在学习时编写的所有目标c教程代码#基本语法正常的printf操作NSlog示例autoRelease池示例#NSString操作NSString操作在这里NSRange示例NSMutableString示例#NSArray示例NSArray示例NSArray函数示例...
通过compare:方法为Foundation中已经支持顺序的类定义了顺序compare:方法: NSString , NSDate , NSNumber , NSIndexPath以及它们的子类,例如NSMutableString和NSDecimalNumber 。 在其他情况下,比较器仅对...
1 通过调用解析类,可以将XML的DATA数据转换为XmlNode对象,XmlNode以树形结构进行XML的数据封装,使用的时候按照树形结构进行数据的获取。(如有问题请留言) XmlNode结构如下: @interface XmlNode : NSObject {...
SSLoggerSSLogger是一个很简单的,轻量级的iOS日志记录工具。使用方法和NSLog类似,可以将日志信息记录到文件中;...NSMutableString * veryLenStr = [NSMutableString stringWithFormat:@""];for