ios中字符串的一些常见操作

1.创建字符串

    NSString *string1 = @"This is a string";//常量字符串
    NSString *string2 = [[NSString alloc] initWithString:@"123"];
    //根据已有字符串创建新的字符串,值是copy的
    NSString *string3 = [NSString stringWithString:string1];
    //格式化字符串,可以在字符串中传递参数
    NSString *string4 = [[NSString alloc] initWithFormat:@"%d--%@",15,@"string"];
    NSString *string5 = [NSString stringWithFormat:@"%d--%@",15,@"string"];
    //创建空字符串
    NSString *string6 = [NSString string];

2.获取字符串中字符的个数,即字符串的长度

    NSString *string1 = @"This is a string";
    NSUInteger strLenth1 = [string1 length];//strLenth1的值为16(包含空格)
    NSUInteger strLenth2 = string1.length;//此处是打点调用,上面的是直接调用方法

3.获取字符串中的某一个字符

    unichar getChar = [@"Hello 世界" characterAtIndex:6];//结果为第7个字符“世”
    NSLog(@"%C",getChar);//注意是%C(如果是%c,则不能输出中文字符)

4.判断一个字符串是否以某个字符串开始或者结束。(当然,该字符串也可以只有一个字符)

    NSString *string1 = @"This is a string";

    //字符串的前缀
    BOOL ok1 = [string1 hasPrefix:@"This"];//结果为真
    BOOL ok2 = [string1 hasPrefix:@"this"];//结果为假。注意区分字符的大小写
    //字符串的后缀
    BOOL ok3 = [string1 hasSuffix: @"g"];//结果为真
    BOOL ok4 = [string1 hasSuffix:@"nG"];//结果为假

5.字符串的合并

    NSString *str1 = @"This is";
    NSString *str2 = @" a string";

    NSString *string1 = [str1 stringByAppendingString:str2];
    //通过格式化字符串的方式合并字符串
    NSString *string2 = [NSString stringWithFormat:@"%@%@",str1,str2];
    NSString *string3 = [[NSString alloc] initWithFormat:@"%@%@", str1,str2];
    //追加一个带格式化的字符串
    NSString *string4 = [str1 stringByAppendingFormat:@"%@--%d",str2,100];

6.字符串的分解(拆分)

    NSString *string = @"This is a string";

    //以空格分隔字符串,分解后是一个NSArray对象
    NSArray *stringArray = [string componentsSeparatedByString:@" "];

7.字符串的截取

    NSString *string = @"This is a string";//注意:string的长度为16,其中的字符下表索引范围是0-15

    NSString *subString1 = [string substringFromIndex:10];//获取从from位置开始到结尾的子字符串
    NSString *subString2 = [string substringToIndex:9];//获取从开始位置到to位置的子字符串
    NSString *subString3 = [string substringWithRange:NSMakeRange(2,5)];//根据指定的范围截取字符串

8.字符串中字符的大小写转换

    NSString *string = @"tHis is a sTring";

    NSString *string1 = [string capitalizedString];//将每个单词的首字母变为大写,其余变小写
    NSString *string2 = [string uppercaseString];//将所有字符变为大写
    NSString *string3 = [string lowercaseString];//将所有字符变为小写

9.字符串与基本数据类型之间的转换(doubleValue、floatValue、intValue、integerValue、longLongValue、boolValue)

    int a = [@"15" intValue];
    float b = [@"12.34" floatValue];
    double c = [@"1111.33333" doubleValue];
    BOOL isOk = [@"1" boolValue];
    long long d = [@"1111111111111" longLongValue];
    NSInteger e = [@"11111" integerValue];

    //int型变量转字符串(其它的基本数据类型均可以通过格式化字符串进行转换)
    NSString *intString = [NSString stringWithFormat:@"%d",a];

10.两个字符串的比较

字符串的比较是按照每个字符的ASCII码进行比较的,返回值是NSComparisonResult类型(实际上是NSInteger类型),共有三种比较结果:

  • NSOrderedAscending(值为-1,代表升序)
  • NSOrderedSame(值为0,代表相同)
  • NSOrderedDescending(值为1,代表降序)
    NSString *string1 = @"Compare String";
    NSString *string2 = @"COMPARE STRING";

    //1.不忽略大小写进行比较
    NSComparisonResult result1 = [string1 string2];
    //2.忽略字符的大小写进行比较
    NSComparisonResult result2 = [string1 caseInsensitiveCompare:string2];
    //3.根据设定的比较选项进行比较
    NSComparisonResult result3 = [string1 compare:string2 options:NSCaseInsensitiveSearch];//此处为忽略大小写进行比较。结果为0,即NSOrderedSame

    //判断两个字符串是否相等
    BOOL isEqual = [string1 isEqualToString:string2];//结果为0,即NO

11.字符串的替换:将可变字符串中的一些字符替换为另一个字符串

    //首先创建一个可变字符串
    NSMutableString *mutableString = [NSMutableString stringWithString: @"mutable string"];
    //将字符串中从第2个字符开始的3个字符(即uta)替换为000000,其它字符保持不变
    [mutableString replaceCharactersInRange:NSMakeRange(1, 3) withString:@"000000"];

12.字符串的插入:将一个字符串插入到一个可变字符串

    NSMutableString *insertString = [NSMutableString stringWithString: @"insert a string"];
    //在第3和第4个字符之间插入一个字符串
    [insertString insertString:@"0000" atIndex:3];

13.可变字符串中字符的删除:根据NSRange来删除可变字符串中的一些字符

    NSMutableString *deleteString = [NSMutableString stringWithString: @"delete some chars in a mutable string"];
    [deleteString deleteCharactersInRange:NSMakeRange(1, 3)];//从第2个字符开始,删除3个字符

14.可变字符串置空

    NSMutableString *mutableStr = [NSMutableString stringWithString: @"a string"];
    mutableStr.string = @"";
    [mutableStr appendString:@"00"];

15.可变字符串后追加字符串

    NSMutableString *mutableStr = [NSMutableString stringWithString: @"This is"];
    [mutableStr appendString:@" a string"];//可以看做是另一种合并字符串的方式

你可能感兴趣的:(IOS基础知识)