(转)Object-C字符串使用总结
字符串常用方法
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// 字符串的创建和初始化
NSString* sFormat = [[NSString alloc] initWithFormat:@"Year:%d", 2009];
NSString* sString = [[NSString alloc] initWithString:sFormat];
NSString* sTmp = nil;
// 获取长度
NSUInteger uLen = [sFormat length];
NSLog(@"uLen = %u", uLen);
// 获取指定索引处的字符
NSUInteger index = 0;
unichar c = [sFormat characterAtIndex:index];
NSLog(@"The character at index %u of \"%@\" is \'%c\'", index, sFormat, c);
// 字符串大小写转换
/*
Case transformations aren’t guaranteed to be symmetrical or to
produce
strings of the same lengths as the originals. The result of this
statement:
lcString = [myString lowercaseString];
might not be equal to this statement:
lcString = [[myString uppercaseString] lowercaseString];
For example, the uppercase form of “ß” in German is “SS”, so
converting
“Straße” to uppercase, then lowercase, produces this sequence of
strings:
“Straße”
“STRASSE”
“strasse”
*/
sTmp = [sFormat lowercaseString];
NSLog(@"Lowercase String of %@ is %@", sFormat, sTmp);
sTmp = [sString uppercaseString];
NSLog(@"Uppercase String of %@ is %@", sString, sTmp);
// 字符串比较
// 地址比较
if (@"Year:2009" == sFormat) {
NSLog(@"The address of sFormat is equal to constant string \"Year:2009\"");
}
else
{
NSLog(@"The address of sFormat isn't equal to constant string \"Year:2009\"");
}
// 内容比较