Objective-C的Foundation框架——字符串

这里要介绍的第一个真正的类是NSString,也就是Cocoa中用来处理字符串的类。

 

0x01 创建字符串

NSstring的stringWithFormat:方法通过格式化字符串和一些参数来输出格式化的结果来创建NSString。

+ (id) stringWithFormat: (NSString *) format, ...;
//最后的省略号表示这个方法可以接受多个以逗号隔开的其他参数
//类似printf()和NSLog()

//如果按如下方式创建一个新的字符串
NSString *height;
height = [NSString stringWithFormat:@"Your height is %d feet, %d inches", 5, 11];
//得到的字符串将是“Your height is 5 feet, 11 inches”

0x02 字符串长度

NSString中有个实例方法length,返回的是字符串中的字符个数。

- (NSUInteger) length;

//使用方法一:
NSUInteger length = [height length];

//使用方法二,直接用在表达式里:
if ([height length] > 35)
{
  NSLog (@"wow, you're really tall!");
}

0x03 字符串的比较

NSString提供了几个用于字符串间比较的方法。

isEqualToString

isEqualToString:方法可以用来比较接收消息的对象和作为参数传递过来的字符串,返回一个BOOL值来表示两个字符串是否相同。

- (BOOL) isEqualToString: (NSString *) aString;

NSString *thing1 = @"hello 5";                                    //直接赋值
NSString *thing2 = [NSString stringWithFormat: @"hello %d", 5];   //通过格式字符串创建
if ([thing1 isEqualToString: thing2])
{
  NSLog (@"They are the same!");
}

比较两个字符串是否相同必须使用isEqualToString:方法!

如果使用if(thing1 == thing2),这里的==运算符只会判断thing1和thing2的指针值是否相等,而不是它们所指的对象是否相等。

要记住,Objective-C里的所有对象间的交互都是通过指针来完成的。

compare

compare:方法将接收对象和传递来的字符串逐个进行比较(区分大小写),返回一个NSComparisonResult(这是一个enum型枚举)来显示比较的结果。

- (NSComparisonResult) compare: (NSString *) aString;

//NSComparisonResult的enum枚举
enum {
  NSOrderedAscending = -1,
  NSOrderedSame,
  NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

compare:options:方法使字符串比较可以有更多的选择。

options参数是一个掩位码,可以使用 | 运算符来添加选项标记,常用选项如下:

  • NSCaseInsensitiveSearch:不区分大小写字符;
  • NSLiteralSearch:进行完全比较,区分大小写字符;
  • NSNumericSearch:比较字符串的字符个数,而不是字符串值,如果不添加这个选项,100就会排在99前面。
if ([thing1 compare: thing2 options: NSCaseInsensitiveSearch | NSNumericSearch]
    == NSOrderedSame)
{
  NSLog (@"They match!");
}

hasPrefix and hasSuffix

hasPrefix:方法用于检查字符串是否以另一个字符串开头;

hasSuffix:方法用于检查字符串是否以另一个字符串结尾。

- (BOOL) hasPrefix: (NSString *) aString;
- (BOOL) hasSuffix: (NSString *) aString;

NSString *fileName = @"draft-chapter.pages";
if ([fileName hasPrefix: @"draft"])
{
  // this is a draft
}
if ([fileName hasSuffix: @".mov"])
{
  // this is a movie
}

rangeOfString

将rangeOfString:发送给一个NSString对象时,传递的参数是要查找的字符串。它将返回一个NSRange结构体,说明与被传递字符串相匹配的部分在哪里以及能够匹配上的字符个数。

如果没有找到对应的部分,range.location的值为NSNotFound。

- (NSRange) rangeOfString: (NSString *) aString;

//示例
NSRange range = [fileName rangeOfString: @"chapter"];

0x04 可变字符串NSMutableString

stringWithCapacity

NSString是不可变字符串,Cocoa提供了一个NSString的子类NSMutableString,它是可变字符串。

由于NSMutableString是NSString的子类,所以可以继承NSString的所有特性:

1、任何使用NSString的地方,都可以用NSMutableString代替;

2、NSString中非常方便的类方法stringWithFormat:也可以用来创建NSMutableString对象。

可以使用类方法stringWithCapacity:来创建一个新的NSMutableString。参数capacity是内存容量,这个容量只是给编译器一个建议值,实际可以超过其大小:

+ (id) stringWithCapacity: (NSUInteger) capacity;

//示例
NSMutableString *string = [NSMutableString stringWithCapacity:42];
NSMutableString *string = [NSMutableString stringWithFormat: @"jo%dy", 2];

appendString and appendFormat

appendString:接收参数aString,然后将其复制到接收对象末尾。

appendFormat:工作方式与stringWithFormat:类似,但没有创建新的字符串,只是将格式化的字符串附加在了接收字符串的末尾。

- (void) appendString: (NSString *) aString;
- (void) appendFormat: (NSString *) format, ...;

//示例
NSMutableString *string = [NSMutableString stringWithCapacity:50];
[string appendString: @"Hello there "];
[string appendFormat: @"human %d!", 39];

//string will have the friendly value “Hello there human 39!”.

deleteCharactersInRange

deleteCharactersInRange:方法用于删除字符串中的字符,可以和rangeOfString:连在一起使用。

- (void) deleteCharactersInRange: (NSRange) aRange;

//示例
NSMutableString *friends = [NSMutableString stringWithCapacity:50];
[friends appendString: @"James BethLynn Jack Evan"];

//find the range of characters where Jack lives:
NSRange jackRange = [friends rangeOfString: @"Jack"];
jackRange.length++; // eat the space that follows

//remove “Jack”
[friends deleteCharactersInRange: jackRange];

//This leaves the string as “James BethLynn Evan”.

猜你喜欢

转载自blog.csdn.net/qq_33737036/article/details/81125057