NSString

<1>常用创建方法
//实例化方法创建

  • (instancetype)initWithString:(NSString *)aString;
  • (instancetype)initWithFormat:(NSString *)format, …;
  • (instancetype)initWithUTF8String:(const char *)bytes;
  • (instancetype)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;现已失效
    //类方法创建
  • (instancetype)stringWithString:(NSString *)aString;
  • (instancetype)stringWithFormat:(NSString *)format,, …;
  • (instancetype)stringWithUTF8String:(const char *)bytes;
  • (instancetype)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;
    <2>求字符串长度
  • (NSUInteger)length;
    <3>通过索引获取相应的字符
  • (unichar)characterAtIndex:(NSUInteger)index;
    <4>字符串比较
    //判断两个字符串是否相等
  • (BOOL)isEqualToString:(NSString *)aString;
    //比较两个字符串大小
  • (NSComparisonResult)compare:(NSString *)aString;
    //不区分大小写比较大小
  • (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString;
    <5>字符串查找
  • (NSRange)rangeOfString:(NSString *)aString;
    <6>判断前后缀
  • (BOOL)hasPrefix:(NSString *)aString;
  • (BOOL)hasSuffix:(NSString �*)aString;
    <7>数字串转化为数字
  • (double)doubleValue;
  • (float)floatValue;
  • (int)intValue;
  • (NSInteger)integerValue ;
  • (long long)longLongValue ;
  • (BOOL)boolValue ;
    <8>大小写转换
  • (NSString *)uppercaseString;
  • (NSString *)lowercaseString;
  • (NSString *)capitalizedString;
    <9>字符串提取
  • (NSString *)substringFromIndex:(NSUInteger)from;
  • (NSString *)substringToIndex:(NSUInteger)to;
  • (NSString *)substringWithRange:(NSRange)range;
    <10>从本地读取文件内容
  • (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
  • (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
    <11>字符串分割
    //按照字符串整体分割
  • (NSArray *)componentsSeparatedByString:(NSString *)separator;
    //按照字符集合分割
  • (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator

1、创建常量字符串。

NSString *astring = @"Welcome to 1000phone";

2、通过实例化方法 initWithString:实例化一个字符串对象

NSString *astring = [[NSString alloc] initWithString:@" I love iOS!"]; NSLog(@"astring:%@",astring);
[astring release];

3、用标准c创建字符串: initWithCString:encoding:方法

const char *cString = "I love iphone";
NSString * aString = [[NSString alloc]initWithCString:cString encoding:NSUTF8StringEncoding];
NSLog(@"astring:%@",aString);
[aString release];
或者:用 initWithUTF8String:实例化一个字符串对象 const char *p = " Welcome to Beijing!";
NSString *string = [[NSString alloc]initWithUTF8String:p];

4、创建格式化字符串:占位符(由一个%加一个字符组成)

int age = 23;
NSString *astring = [[NSString alloc] initWithFormat:@”I am %d”,age]]; NSLog(@"astring:%@",astring);
[astring release];

5、通过静态方法创建字符串对象

NSString * str1 = [NSString stringWithString:@"I love programming!"];
NSString * str2 = [NSString stringWithUTF8String:" I love programming!"]; NSString * str3 = [NSString stringWithFormat:@"%@",@" I love programming!"];

6. 从文件读取字符串:initWithContentsOfFile:

NSString *path = @" /Users/qianfeng/Desktop/StringAPI.pdf"; //文件路径 NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; NSLog(@"astring:%@",astring);
[astring release];

/----------------比较两个字符串----------------/ //用 C 比较:strcmp 函数

char string1[] = "string!";
char string2[] = "string!"; if(strcmp(string1, string2) = = 0) {
NSLog(@"1"); }

1. isEqualToString 方法 判断两个字符串是否相等 相等返回 1 不等返回 0

NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02]; NSLog(@"result:%d",result);

2. compare 方法(compare:返回的三种值 分别是-1,0,1)

//NSOrderedSame 判断两者内容是否相同
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedSame;//0 NSLog(@"result:%d",result);
//NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring01 小于 astring02 为真)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedAscending; NSLog(@"result:%d",result); //
//NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring01 大于 astring02 为真)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending; NSLog(@"result:%d",result);

3. 不考虑大小写比较字符串

NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
enum _NSComparisonResult {
NSOrderedAscending = -1, // astring01 小于 astring02 NSOrderedSame, astring01 等于 astring02 NSOrderedDescending astring01 大于 astring02 };

/----------------改变字符串的大小写----------------/

NSString *string1 = @"A String";
NSString *string2 = @"String";

1. uppercaseString 小写转大写 string1 指向的对象内容不会发生改变 (会产生一个新的字符串对象,string 指向这个新的对象)

NSString *string = [string1 uppercaseString];
NSLog(@"string:%@",string);

2. lowercaseString 大写转小写 string = [string1 lowercaseString]; NSLog(@"string:%@",string);

3. capitalizedString 单词首字母大写 其余小写,其他字符保持不变 NSLog(@"string:%@",string);//首字母大小

/----------------搜索子串----------------/

NSString *string1 = @"I love iOS very much!";
NSString *string2 = @"iOS";
NSRange range = [string1 rangeOfString:string2]; //NSRange 代表一个范围:子 串 string2 在长串 string1 中的范围(位置和长度)
NSUInteger location=range.location;
NSUInteger length= range.length;
NSString *astring = [NSString stringWithFormat:@"Location:%lu,Leight:%lu",location, length]; NSLog(@"astring:%@",astring);
[astring release];

/----------------抽取子串 ----------------/

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

置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3]; NSLog(@"string2:%@",string2);

2. -substringFromIndex: 从指定位置开始(包括指定位置的字符)提取,并包 括之后的全部字符 直到最后;

NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3]; NSLog(@"string2:%@",string2);

3. -substringWithRange: //在指定范围内从字符串中截取子串

NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)]; NSLog(@"string2:%@",string2);

/------------判断字符串内是否还包含别的字符串(前缀,后缀)------------/ ##01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *)

aString; (判断前缀)
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); ##02:检查字符串是否以另一个字符串结尾- (BOOL) hasSuffix: (NSString *) aString; (判断后缀)
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");

二.NSMutableString

<1>指定索引插入子串

  • (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;
    <2>删除指定范围的子串
  • (void)deleteCharactersInRange:(NSRange)range;
    <3>追加子串
  • (void)appendString:(NSString *)aString;
    <4>追加格式化子串
  • (void)appendFormat:(NSString *)format, ... ;
    <5>修改字符串 覆盖原有内容
  • (void)setString:(NSString *)aString;

/---------------给字符串分配预设空间大小----------------/ //stringWithCapacity:

NSMutableString *string;
string = [NSMutableString stringWithCapacity:20];//预设一个 20 字节的字符串 空间
string.string = @”Welcome to qianfeng”;

/---------------增加字符串----------------/

1. appendString: appendFormat: 追加,在后面追加字符串

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
//[String1 appendString:@", I will be adding some characters"];
[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some characters"]];
NSLog(@"String1:%@",String1);

2. -insertString: atIndex: 在指定位置插入字符串

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);

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

//deleteCharactersInRange: 在指定范围内删除字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a
NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)]; NSLog(@"String1:%@",String1);

/--------修改字符串------/ //-setString: 修改字符串

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);

/--------在指定范围内,替换的原有的字符------/ // replaceCharactersInRange: withString: 替换

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; NSLog(@"String1:%@",String1);

读写文件
从本地读取文件内容

  • (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
  • (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;

NSString * readResult = [[NSString alloc]initWithContentsOfFile:@"/Users/apple/Desktop/test.txt" encoding:NSUTF8StringEncoding error:nil];
[readResult writeToFile:@"/Users/apple/Desktop/file.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
删除某子串
NSMutableString * s = [[NSMutableString alloc]initWithFormat:@"%d年之后,你不让你是我",10];
NSRange range = NSMakeRange(2, 3);
[s deleteCharactersInRange:range];

字符串和文件相关的 方法
NSString * filePath = @"/Users/apple/Desktop/qfile.rar";
//取出文件的后缀名
NSString * extension = [filePath pathExtension];
//取文件名称
NSString * lastPart =[filePath lastPathComponent];

你可能感兴趣的:(NSString)