IOS疯狂基础之Objective-c 字符串操作 含NSLog的说明
来源:互联网 发布:三维人像数据门系统 编辑:程序博客网 时间:2024/06/05 17:02
前言:本人身为程序员,对代码一向是有则抄之,无则编之。
关于trim
NSString * temp = [levelName.text stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];
[@" abc 123 " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
关于 replace 替换
在NSString中,需要用一个字符代替NSString字符串里面的某个特别的字符,此时使用
[NSString stringByReplacingOccurrencesOfString: withString:];
而在string中,需要用一个字符代替string字符串里面的某个特别的字符,此时使用
[string replaceOccurrencesOfString:(NSString *) withString:(NSString *)]
NSMutableString *new = [[NSMutableStringalloc]initWithString:[requestresponseString]];
[newreplaceCharactersInRange:NSMakeRange(0, new.length-1)withString:@""""];//替换指定范围
NSLog(@"字符串处理");
对NSLog的警告进行解释下:
NSLog(str); // warning
NSLog(@"%@", str); // solution
NSLog(str, nil); // solution
总之 要么格式化,就么后面加个nil
//获得字符串长度
NSString* str1=@"MAC OS Pro";
NSLog(@"长度是 %d",[str1length]);
//两个字符串连接
NSString* str2=@"MAC OS Pro";
NSString* str3=@" Leopard";
NSString* str4=[str2stringByAppendingString:str3];
NSLog(str4,NULL);
NSString* str5=[str2stringByAppendingFormat:@" %d",10];
NSLog(str5,nil);
//可修改字符串
NSMutableString* string=[NSMutableStringstring]; //相当于c#的StringBuilder
[string appendString:@"Mac"];
[string appendString:@" OS"];
[string appendString:@" X"];
NSLog(string,nil);
//插入字符串
NSMutableString* string1=[NSMutableStringstringWithFormat:@"Mac X"];
[string1 insertString:@"OS "atIndex:4];
NSLog(string1,nil);
//删除字符串
NSMutableString* string2=[NSMutableStringstringWithFormat:@"删除字符串"];
[string2 deleteCharactersInRange:NSMakeRange(0,2)];
NSLog(string2,nil);
//字符串比较
BOOL result;
result=[string isEqualToString:string1];
NSLog(@"%d",result,nil);
//比较前置串与后置串
NSString* string3=@"Mac Pro";
NSString* string4=@"Mac OS";
//判断是否以Mac开头
result=[string3 hasPrefix:@"Mac"];//妹的这个方法名真的是我英文不通关还是怎么的
NSLog(@"%d",result,nil);
//判断是否以OS结尾
result=[string4 hasSuffix:@"OS"];
NSLog(@"%d",result,nil);
NSString* string5=@"This is Mac OS X";
//检索字符串
NSRange range;
range=[string5 rangeOfString:@"Mac OS"];
if (range.location!=NSNotFound)
{
//得到字符串的位置和长度
NSLog(@"%d,%d",range.location,range.length);
}
else
{
NSLog(@"没有发现",nil);
}
//抽取部分字符串
NSString* string6=@"This is Mac OS X";
NSString *nstr1,*nstr2,*nstr3;
//从开头抽取字符串
nstr1=[string6 substringToIndex:4];
NSLog(nstr1,nil);
//抽取末位位置的字符串
nstr2=[string6 substringFromIndex:8];
NSLog(nstr2,nil);
//抽取指定范围的字符串
nstr3=[string6 substringWithRange:NSMakeRange(5,2)];
NSLog(nstr3,nil);
- IOS疯狂基础之Objective-c 字符串操作 含NSLog的说明
- Objective-C之NSLog
- Objective-C基础之字符串
- Objective-C的NSLog方法
- Objective-C之NSLog和BOOL的整理。
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- iOS基础之OC:C与OC的对比,(NSString,NSLog)
- Objective-C 之NSLog 输出格式
- Objective-C学习笔记-之NSLog()
- Objective-c NSLog 的宏定义
- 【006】Objective-C中NSLog的格式
- 查看设置本机共享文件 net share
- 关注C++细节——STL中map学习小结
- hdu 1576 A/B || hdu 2669 Romantic (数论--欧几里德)
- JavaScript的=、==和===
- (EM算法)The EM Algorithm
- IOS疯狂基础之Objective-c 字符串操作 含NSLog的说明
- ubuntu开机自动设置屏幕亮度
- 代码评审:Just do it!
- FLX6410上调试Marvell 88W8787 SDIO WIFI
- sqlite3操作数据库
- 2012年总结
- 在FLX6410上调试88W8787的AP模式
- 三色球问题
- Spring中Bean属性为集合的注入方式