objective-c 字符串各种处理

内容简要:

1、创建常量字符串。

<wbr style="line-height:28px">2</wbr>、创建空字符串,给予赋值。

3、在以上方法中,提升速度:initWithString方法

4、用标准c创建字符串:initWithCString方法。

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

6、创建临时字符串。

7、判断字符串为空。

9、是否以”test”开头;是否以”.move”结尾。

10、比较两个字符串。

11、声明一个可变字符;长度是40个字符。

12、修改可变字符;先声明一个可变字符myFriend;长度30。

13、在一个字符串后面附加一个新的字符串。

14、字符串转换整数值。

15、从文件读取字符串:initWithContentsOfFile方法。

16、写字符串到文件:writeToFile方法

17、改变字符串的大小写。

18、在串中搜索子串。

19、抽取子串。

20、扩展路径。

21、文件扩展名。

22、在已有字符串后面添加字符。

23、在已有字符串中按照所给出范围和长度删除字符。

24、在已有字符串后面在所指定的位置中插入给出的字符串。

25、将已有的空符串换成其它的字符串。

26、按照所给出的范围,和字符串替换的原有的字符。

27、判断字符串内是否还包含别的字符串(前缀,后缀)。


----------------------------------------------------------------------------------------------------------------------------------------------------

1、创建常量字符串。
<wbr style="line-height:28px">NSString *astring = @"This is a String!";</wbr>


<wbr style="line-height:28px">2</wbr>、创建空字符串,给予赋值。

<wbr style="line-height:28px">NSString *astring = [[NSString alloc] init];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px">astring = @"This is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px">NSLog(@"astring:%@",astring);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px">[astring release];</wbr></wbr></wbr></wbr>


3、在以上方法中,提升速度:initWithString方法

<wbr style="line-height:28px">NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px">NSLog(@"astring:%@",astring);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px">[astring release];</wbr></wbr></wbr>


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

char *Cstring = "This is a String!";
NSString *astring = [[NSString alloc] initWithCString:Cstring];
NSLog(@"astring:%@",astring);
[astring release];


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

<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">int i = 1;<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">int j = 2;<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"astring:%@",astring);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[astring release];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

6、创建临时字符串

<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring;<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">astring = [NSString stringWithCString:"This is a temporary string"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"astring:%@",astring);<wbr style="line-height:28px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


7、判断字符串为空

NSString *urlString = [urlInput stringValue];
if (!urlString) {<wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>NSLog( @”NO INPUT.” );<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>}<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>if ([urlString length] == 0 ) {<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>NSLog( @”NO INPUT.” );<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>}<wbr style="line-height:28px"></wbr></wbr>




9、是否以”test”开头;是否以”.move”结尾;

NSString *fileName = @”test.move”;

  if ([fileName hasPrefix:@"test"]) {

  NSLog(@”has Test String !”);

  }else{

  NSLog(@”don’t have Test”);

  }


  [fileName hasSuffix:@".move"]?NSLog(@”Yes it got a .Mov in its end”):NSLog(@”no it has no .mov string”);




10、比较两个字符串:

strcmp函数

<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">char string1[] = "string!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">char string2[] = "string!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">if(strcmp(string1, string2) = = 0)<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">{<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"1");<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

isEqualToString方法<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring01 = @"This is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring02 = @"This is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">BOOL result = [astring01 isEqualToString:astring02];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"result:%d",result);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


compare方法(comparer返回的三种值)<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring01 = @"This is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring02 = @"This is a String!";<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">BOOL result = [astring01 compare:astring02] = = NSOrderedSame;<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"result:%d",result);<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px">NSOrderedSame<span style="word-wrap:normal; word-break:normal">判断两者内容是否相同</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"></wbr></wbr></wbr></wbr>

<wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring01 = @"This is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring02 = @"this is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"result:%d",result);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:28px"><wbr style="line-height:28px"></wbr></wbr>NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)



<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring01 = @"this is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring02 = @"This is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"result:%d",result);<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px">NSOrderedDescending<span style="word-wrap:normal; word-break:normal">判断两对象值的大小</span>(<span style="word-wrap:normal; word-break:normal">按字母顺序进行比较,</span>astring02<span style="word-wrap:normal; word-break:normal">小于</span>astring01<span style="word-wrap:normal; word-break:normal">为真</span>)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>



不考虑大小写比较字符串1
<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring01 = @"this is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring02 = @"This is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"result:%d",result);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)


不考虑大小写比较字符串2
<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring01 = @"this is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring02 = @"This is a String!";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">BOOL result = [astring01 compare:astring02<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"result:%d",result);<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>NSCaseInsensitiveSearch:<span style="word-wrap:normal; word-break:normal">不区分大小写比较</span>NSLiteralSearch:<span style="word-wrap:normal; word-break:normal">进行完全比较,区分大小写</span>NSNumericSearch:<span style="word-wrap:normal; word-break:normal">比较字符串的字符个数,而不是字符值。</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


11、声明一个可变字符;长度是40个字符;

  NSMutableString *myMutableString;

  myMutableString = [NSMutableString stringWithCapacity:40];

  NSString *myName = @”Leo”;

  

  [myMutableString appendString:@"Hello ,there"];

  [myMutableString appendFormat:@" i am %@",myName];

  NSLog(@”this is NSMutableString: %@”,myMutableString);

  //this is NSMutableString: Hello ,there i am Leo;

  

12、修改可变字符;先声明一个可变字符myFriend;长度30;


  NSMutableString *myGirlFriend;

  myGirlFriend = [NSMutableString stringWithCapacity:30];

  //然后给字符加入一些内容;

  [myGirlFriend appendString:@"Here are my GF:Carol Sophia Ashley Helen and Yoyo"];

  NSLog(@”%@”,myGirlFriend);

  //声名一个变动范围(NSRange);

  NSRange joneRange;

  joneRange = [myGirlFriend rangeOfString:@"Helen "];

  //下面:就是从myFriend字符中配对,如果有相等的内容就删除了;

  [myGirlFriend deleteCharactersInRange:joneRange];

  NSLog(@”%@”,myGirlFriend);

13、在一个字符串后面附加一个新的字符串

NSString *a = @"a";

NSString *b = [a stringByAppendingString:@"b"];//b变量的值为“ab”

14、字符串转换整数值

NSString *age = @"36";

if([age intValue]>35){

}

15、从文件读取字符串:initWithContentsOfFile方法
<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *path = @"astring.text";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring = [[NSString alloc] initWithContentsOfFile:path];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"astring:%@",astring);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[astring release];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

16、写字符串到文件:writeToFile方法
<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"astring:%@",astring);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *path = @"astring.text";<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[astring writeToFile: path atomically: YES];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[astring release];<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


17、改变字符串的大小写

<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string1 = @"A String";<wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string2 = @"String";<wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"string1:%@",[string1 uppercaseString]);//<span style="word-wrap:normal; word-break:normal">大写</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"string2:%@",[string2 lowercaseString]);//<span style="word-wrap:normal; word-break:normal">小写</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"string2:%@",[string2 capitalizedString]);//<span style="word-wrap:normal; word-break:normal">首字母大小</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


18、在串中搜索子串

<wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string1 = @"This is a string";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string2 = @"string";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSRange range = [string1 rangeOfString:string2];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">int location = range.location;<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">int leight = range.length;<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"astring:%@",astring);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[astring release];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


19、抽取子串

<wbr style="line-height:28px"><wbr style="line-height:28px"></wbr></wbr>//-substringToIndex:从字符串的开头一直截取到指定的位置,但不包括该位置的字符
<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string1 = @"This is a string";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string2 = [string1 substringToIndex:3];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"string2:%@",string2);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">//-substringFromIndex:<span style="word-wrap:normal; word-break:normal">以指定位置开始(包括指定位置的字符),并包括之后的全部字符</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string1 = @"This is a string";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string2 = [string1 substringFromIndex:3];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"string2:%@",string2);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">//-substringWithRange: //<span style="word-wrap:normal; word-break:normal">按照所给出的位置,长度,任意地从字符串中截取子串</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string1 = @"This is a string";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"string2:%@",string2);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>20<span style="word-wrap:normal; word-break:normal">、扩展路径</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *Path = @"~/NSData.txt";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *absolutePath = [Path stringByExpandingTildeIn<wbr style="line-height:28px">Path];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"absolutePath:%@",absolutePath);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWith<wbr style="line-height:28px">TildeInPath]);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>21<span style="word-wrap:normal; word-break:normal">、文件扩展名</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *Path = @"~/NSData.txt";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"Extension:%@",[Path pathExtension]);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>22<span style="word-wrap:normal; word-break:normal">、在已有字符串后面添加字符</span><wbr style="line-height:28px"><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">//[String1 appendString:@", I will be adding some character"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"String1:%@",String1);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"><br style="line-height:28px"></span>23<span style="word-wrap:normal; word-break:normal">、在已有字符串中按照所给出范围和长度删除字符</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[String1 deleteCharactersInRange:NSMakeRange(0, 5)];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"String1:%@",String1);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><span style="word-wrap:normal; word-break:normal">24、在已有字符串后面在所指定的位置中插入给出的字符串</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">//-insertString: atIndex:<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[String1 insertString:@"Hi! " atIndex:0];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"String1:%@",String1);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span>25<span style="word-wrap:normal; word-break:normal">、将已有的空符串换成其它的字符串</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">//-setString:<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[String1 setString:@"Hello Word!"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"String1:%@",String1);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><span style="word-wrap:normal; word-break:normal">26、按照所给出的范围,和字符串替换的原有的字符</span><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">//-setString:<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[String1 replaceCharactersInRange<wbr style="line-height:28px">:NSMakeRange(0, 4) withString:@"That"];<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSLog(@"String1:%@",String1);<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><span style="word-wrap:normal; word-break:normal">27、判断字符串内是否还包含别的字符串</span>(<span style="word-wrap:normal; word-break:normal">前缀,后缀</span>)<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">//01<span style="word-wrap:normal; word-break:normal">:检查字符串是否以另一个字符串开头</span>- (BOOL) hasPrefix: (NSString *) aString;<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">NSString *String1 = @"NSStringInformation.txt";<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[String1 hasPrefix:@"NSString"] = = 1 ?<wbr style="line-height:28px">NSLog(@"YES") : NSLog(@"NO");<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">[String1 hasSuffix:@".txt"] = = 1 ?<wbr style="line-height:28px">NSLog(@"YES") : NSLog(@"NO");<span style="word-wrap:normal; word-break:normal"><br style="line-height:28px"></span><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px">//02<span style="word-wrap:normal; word-break:normal">:查找字符串某处是否包含其它字符串</span>- (NSRange) rangeOfString: (NSString *) aString<span style="word-wrap:normal; word-break:normal">,这一点前面在串中搜索子串用到过</span>;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值