NSString *str1 = @"aBcDeFgHiJk";

NSString *str2 = @"12345";

NSString *res;

NSComparisonResult compareResult;

NSRange subRange;

//字符个数

NSLog(@"字符串str1长度: %i",[str1 length]);

//拷贝字符串到res

res = [NSString stringWithString: str1];

NSLog(@"拷贝: %@", res);

//拷贝字符串到str1尾部

str2 = [str1 stringByAppendingString: str2];

NSLog(@"连接字符串: %@", str2);

//测试字符串相等

if ([str1 isEqualToString: res] == YES) {

NSLog(@"str1 == res");

} else {

NSLog(@"str1 != res");

}

//测试字符串 < > == 

compareResult = [str1 compare: str2];

if (compareResult == NSOrderedAscending) {

NSLog(@ "str1 < str2");

} else if (compareResult == NSOrderedSame) {

NSLog(@ "str1 == str2");

} else {

NSLog(@ "str1 > str2");

}

res = [str1 uppercaseString];

NSLog(@"大写字符串:%@", res);

res = [str1 lowercaseString];

NSLog(@"小写字符串:%@", res);

NSLog(@"原始字符串: %@", str1);

//获得前三个数

res = [str1 substringToIndex: 3];

NSLog(@"字符串str1的前三个字符: %@",res);

res = [str1 substringFromIndex: 4];

NSLog(@"截取字符串,从第索引4到尾部: %@",res);

res = [[str1 substringFromIndex: 3] substringToIndex: 2];

NSLog(@"截取字符串,从第索引35: %@",res);

//字符串查找

subRange = [str2 rangeOfString: @"34"];

if (subRange.location == NSNotFound) {

NSLog(@"字符串没有找到");

} else {

NSLog (@"找到的字符串索引 %i 长度是 %i"

  subRange.location, subRange.length);

}

2012-10-09 11:00:31.955 2.10.2-1[27147:403] 字符串str1长度: 11

2012-10-09 11:00:31.958 2.10.2-1[27147:403] 拷贝: aBcDeFgHiJk

2012-10-09 11:00:31.959 2.10.2-1[27147:403] 连接字符串: aBcDeFgHiJk12345

2012-10-09 11:00:31.960 2.10.2-1[27147:403] str1 == res

2012-10-09 11:00:31.961 2.10.2-1[27147:403] str1 < str2

2012-10-09 11:00:31.962 2.10.2-1[27147:403] 大写字符串:ABCDEFGHIJK

2012-10-09 11:00:31.963 2.10.2-1[27147:403] 小写字符串:abcdefghijk

2012-10-09 11:00:31.964 2.10.2-1[27147:403] 原始字符串: aBcDeFgHiJk

2012-10-09 11:00:31.965 2.10.2-1[27147:403] 字符串str1的前三个字符: aBc

2012-10-09 11:00:31.965 2.10.2-1[27147:403] 截取字符串,从第索引4到尾部: eFgHiJk

2012-10-09 11:00:31.967 2.10.2-1[27147:403] 截取字符串,从第索引35: De

2012-10-09 11:00:31.967 2.10.2-1[27147:403] 找到的字符串索引 13 长度是 2

 

 

 

NSString *str1 = @"Objective C";

NSString *search,*replace;

NSMutableString *mstr;

NSRange substr;

//从不可变的字符创建可变字符串对象

mstr = [NSMutableString stringWithString: str1];

NSLog(@" %@", mstr);

//插入字符串

[mstr insertString: @ " Java" atIndex: 9];

NSLog(@" %@", mstr);

//具有连接效果的插入字符串

[mstr insertString: @ " and C++"atIndex: [mstr length]];

NSLog(@" %@", mstr);

//字符串连接方法

[mstr appendString: @ " and C"];

NSLog(@" %@", mstr);

//使用NSRange删除字符串

[mstr deleteCharactersInRange:NSMakeRange(16, 13)];

NSLog(@" %@", mstr);

//查找字符串位置

substr = [mstr rangeOfString: @ "string B and"];

if (substr.location != NSNotFound) {

[mstr deleteCharactersInRange: substr];

NSLog(@" %@", mstr);

}

//直接设置可变字符串

[mstr setString: @ "This is string A "];

NSLog(@" %@", mstr);

[mstr replaceCharactersInRange: NSMakeRange(8, 8)

withString: @ "a mutable string "];

NSLog(@" %@", mstr);

//查找和替换

search = @"This is ";

replace = @"An example of ";

substr = [mstr rangeOfString:search];

if (substr.location != NSNotFound) {

[mstr replaceCharactersInRange:substr withString: replace];

NSLog(@" %@", mstr);

}

//查找和替换所有的情况

search = @"a";

replace = @"X";

substr = [mstr rangeOfString: search];

while (substr.location != NSNotFound) {

[mstr replaceCharactersInRange:substr withString: replace];

substr = [mstr rangeOfString: search];

}

NSLog(@" %@", mstr);

2012-10-09 10:55:21.925 2.10.2-2[27120:403]  Objective C

2012-10-09 10:55:21.928 2.10.2-2[27120:403]  Objective Java C

2012-10-09 10:55:21.929 2.10.2-2[27120:403]  Objective Java C and C++

2012-10-09 10:55:21.930 2.10.2-2[27120:403]  Objective Java C and C++ and C

2012-10-09 10:55:21.930 2.10.2-2[27120:403]  Objective Java CC

2012-10-09 10:55:21.931 2.10.2-2[27120:403]  This is string A 

2012-10-09 10:55:21.932 2.10.2-2[27120:403]  This is a mutable string  

2012-10-09 10:55:21.933 2.10.2-2[27120:403]  An example of a mutable string  

2012-10-09 10:55:21.934 2.10.2-2[27120:403]  An exXmple of X mutXble string