标签:
OC中的字符串:
C中没有字符串类型,用字符数组和指针代替。
OC中引入了字符串类型,它包括NSString 和 NSMutableString两种
NSString是不可变的,已经初始化便不能更改;NSMutableString是可变的,可以对自己进行修改操作的。这类似于Java里的String和BufferString。
NSString字符串的初始化方法:
//通过对象方法(动态方法) NSString * str1 = [[NSString alloc]initWithUTF8String:"wang1"]; NSString * str2 = [[NSString alloc]initWithString:@"wang2"]; //通过类方法(静态方法) NSString * str3 = [NSString stringWithUTF8String:"wang3"]; NSString *str4 =[NSString stringWithString:"wang4"];
NSString字符串常用方法:
// // main.m // NSStringTest // // Created by mac on 15-2-25. // Copyright (c) 2015年 mac. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSString *str = @"wang1.avi"; //根据索引获取字符串元素 UniChar ch = [str characterAtIndex:0]; NSLog(@"%c",ch); //字符串内容比较 BOOL tag = [str isEqualToString:@"wang1.mp4"]; NSLog(@"%i",tag); if (tag==NO) { NSLog(@"butong"); }else { NSLog(@"xiangtong"); } //字符串大小比较 NSComparisonResult ret = [str compare:@"wang2.avi"]; if(ret==NSOrderedAscending) //wang1.avi到wang2.avi是增序的 { NSLog(@"up"); }else if(ret==NSOrderedDescending) { NSLog(@"down"); }else { NSLog(@"equal"); } //查找子串的范围range(位置,长度) NSRange range = [str rangeOfString:@"wang1"]; NSLog(@"%lu,%lu",range.location,range.length); //判断前后缀 BOOL tag1 = [str hasPrefix:@"wang"]; if (tag1==YES) { NSLog(@"是以wang为前缀"); } BOOL tag2 = [str hasSuffix:@"avi"]; if (tag2==YES) { NSLog(@"是以avi为后缀"); } //数组串转换成数字 NSString *intStr = @"1031"; NSString *floatStr = @"3.14"; int intNum = [intStr intValue]; float floatNum = [floatStr floatValue]; NSLog(@"_____________%f",intNum+floatNum); NSLog(@"%i,%f",intNum,floatNum); //大小写转换 NSString *upperStr = [str uppercaseString]; //把wang1.avi转为大写WANG1.AVI NSString *lowerStr = [upperStr lowercaseString]; //再把WANG1.AVI转为wang1.avi //字符串提取(获取子串) NSString *subStr1 = [str substringFromIndex:5]; //从索引5开始到结尾 NSLog(@"%@",subStr1); NSString *subStr2 = [str substringToIndex:4]; //从开头到索引4 // NSRange range1; // range1.location = 5; // range1.length = 4; NSRange range1 = NSMakeRange(5, 4); //注意:从格式就可以看出,NSMakeRange(5,4)并不是方法,它只是宏替换。 NSString *subStr3 = [str substringWithRange:range1]; NSLog(@"%@__%@",subStr2,subStr3); } return 0; }
NSMutableString字符串的常用方法:
首先要说的时NSMutableString是继承于NSString的,所以NSString的所有方法都可以用于NSMutableString字符串。但NSMutableString不同之处是,它定义了很多可以最自己进行操作的方法:
// // main.m // NSMutableString // // Created by mac on 15-2-26. // Copyright (c) 2015年 mac. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //NSMutableString字符串不能这样创建 // NSMutableString * mutStr = @"wanger"; // NSMutableString * mutStr = [[NSMutableString alloc]initWithUTF8String:"wanger"]; NSMutableString * mutStr = [NSMutableString stringWithString:@"wang"]; NSString *str = [NSString stringWithUTF8String:".avi"]; //增(拼接) [mutStr appendString:@".avi"]; // [mutStr appendFormat:@"%@",str]; [mutStr insertString:@"er" atIndex:4]; NSLog(@"%@",mutStr); //删 NSRange range1 = NSMakeRange(2, 3); [mutStr deleteCharactersInRange:range1]; //改 [mutStr setString:@"wangyawen.avi"]; //重置可变字符串内存中的内容 //替换 NSRange range2 = NSMakeRange(10, 3); [mutStr replaceCharactersInRange:range2 withString:@"mp4"]; //替换 NSLog(@"%@",mutStr); } return 0; }
---恢复内容结束---
标签:
原文地址:http://www.cnblogs.com/wangerxiansheng/p/4301123.html