//字符串的创建与使用
#import<Foundation/Foundation.h>
int main( int argc, const char argv[])
{
@autoreleasepool{
//创建一个字符串常量
NSString *string1 = @ "Hello" ;
//创建一个空的字符串
NSString *string2 = [[NSString alloc] init];
NSString *string3 = [NSString string];
//快速创建一个字符串(常量区)
NSString *string4 = [[NSString alloc] initWithString:@ "Hello" ];
NSString *string5 = [NSString stringWithString:@ "Hello" ];
NSString *string8 = [NSString stringWithString:@ "Hello" ];
//快速创建一个格式化字符串(堆区)
int number = 2;
NSString *string6 = [[NSString alloc] initWithFormat:@ "%d" , number];
NSString *string7 = [NSString stringWithFormat:@ "浮点数:%f" ,10.8];
NSString *string9 = [NSString stringWithFormat:@ "浮点数:%f" ,10.8];
//测试字符串内容是否相同
if ([string6 isEqualToString:string7])
NSLog(@ "Same content" );
else
NSLog(@ "Different content" );
//输出:Different content
if ([string4 isEqualToString:string5])
NSLog(@ "Same content" );
else
NSLog(@ "Different content" );
//输出:Same content
//测试两个字符串是否为同一对象
if (string5 == string8)
NSLog(@ "Same point" );
else
NSLog(@ "Different point" );
//输出:Same point
if (string7 == string9)
NSLog(@ "Same point" );
else
NSLog(@ "Different point" );
//输出:Different point
//比较字符串的大小
NSString *string10 = [[NSString alloc] initWithFormat:@ "a" ];
NSString *string11 = [[NSString alloc] initWithFormat:@ "b" ];
NSComparisonResult result = [string10 caseInsensitiveCompare:string11];
NSLog(@ "%d" , result);
//输出:-1 (升序)
//求字符串的长度
NSLog(@ "%d" , [string11 length]);
//输出:1
//字符串大小转换
NSString *string12 = @ "Hello wORld" ;
NSLog(@ "upper: %@" , [string12 uppercaseString]); //全部转换成大写
NSLog(@ "lower: %@" , [string12 lowercaseString]); //全部转换成小写
NSLog(@ "capital: %@" , [string12 capitalizedString]); //首字母大写
//输出:HELLO WORLD
hello world
Hello World
//转换成基本数据类型。。。。。
NSString *string13 = @ "3.14" ;
float pi = [string13 floatValue];
NSLog(@ "pi : %.2f" , pi);
//输出:3.14
//字符串转换成数组
NSString *string14 = @ "abc xyz" ;
NSArray *array = [string14 componentsSeparatedByString:@ " " ); //以空格为间隔
NSLog(@ "%@" , array);
//输出:abc
xyz
//截取字符串
NSString *string15 = @ "abcde" ;
NSString *subString1 = [string15 substringToIndex:2]; //截取到底2位
NSString *subString2 = [string15 substringFromIndex:2]; //从第2位开始截取
NSLog(@ "%@" , subString1);
NSLog(@ "%@" , subString2);
//输出:ab
cde
NSRange range; //NSRange range = {3, 2};
range.location = 3;
range.length = 2;
NSString *subString3 = [string15 substringWithRange:range];
NSLog(@ "%@" , subString3);
//输出:de
//字符串的拼接
NSString *string16 = @ "abc" ;
NSString *string17 = @ "xyz" ;
NSString *appString1 = [[NSString alloc] initWithFormat:@ "%@%@" , string16, stirng17];
NSString *appString2 = [string16 stringByAppendingFormat:@ "%@" , string17];
NSString *appString3 = [string16 stringByAppendingString:string17];
NSLog(@ "%@, %@, %@" , appString1, appString2, appString3);
//输出:abcxyz, abcxyz, abcxyz
//查找字符串
NSString *link = @ "abcdeftarget=_blankxyz" ;
NSRange range1 = [link rangeOfString:@ "target=_blank" ];
NSLog(@ "%@" , NSStringFromRange(range1));
//输出:(6,13)
if (range1.location != NSNotFound)
NSLog(@ "Founded" );
//输出:Founded
/******************可变字符串操作*************************/
NSMutableString *mutableString1 = [[NSMutableString alloc] initWithFormat:@ "abc" ];
//插入
[mutableString1 insertString:@ "...xyz" atIndex:3];
NSLog(@ "%@" , mutableString1);
//输出:abc...xyz
//替换
[mutableString1 replaceCharactorsInRange:NSMakeRange(0,3) withString:@ "efg" ];
NSLog(@ "%@" , mutableString1);
//输出:efg...xyz
//删除
[mutableString1 deleteCharactorsInRange:NSMakeRange(0,3)];
NSLog(@ "%@" , mutableString1);
//输出:...xyz
|