zoukankan      html  css  js  c++  java
  • NSString

    // —————————NSString

            // NSString

            //1.初始化:直接字符串、格式化字符串、文件读取内容初始化

            NSString *str1 = @"hello world!";

            NSString *str2 = [NSString stringWithFormat:@"start to printf: %@", str1];

            NSString *str3 = [[NSString alloc] initWithContentsOfFile:@"/user/document/test.txt" encoding:NSUTF8StringEncoding error:nil];

            

            // 字符串写入文件

            [str1 writeToFile:@"/user/document/test.txt" atomically:YES];

            

            // 字符串大写、小写、首字母大写

            str1 = [str1 uppercaseString];

            str1 = [str1 lowercaseString];

            str1 = [str1 capitalizedString];

            

            //字符串截取

            str2 = [str1 substringFromIndex:5];

            NSLog(@"string = %@", str2);

            str2 = [str1 substringToIndex:5];

            NSLog(@"string = %@", str2);

            str2 = [str1 substringWithRange:NSMakeRange(0, 7)];

            NSLog(@"string = %@", str2);

            // 字符串是否以另一字符串开头或结尾

            BOOL isStart = [str1 hasPrefix:@"hello"];

            NSLog(@"isStart is : %i", isStart);

            BOOL isEnd = [str1 hasSuffix:@"world!"];

            NSLog(@"isEnd is : %i", isEnd);

            

            // 获取文件后缀名

            NSString *filePath = @"/document/test.txt";

            NSString *ext = [filePath pathExtension];

            NSLog(@"ext is : %@", ext);

            

            // 字符串转整形

            NSString *value = @"100";

            int va = [value intValue];

            NSLog(@"va is : %i", va);

            // 字符串替换

            str1 = [str1 stringByReplacingOccurrencesOfString:@"hello" withString:@"bye bye"];

            // 字符串转整

            int intVal = [str1 intValue];

            NSLog(@"str to int : intVal= %i", intVal);

            str1 = [[NSNumber numberWithInteger:intVal] stringValue];

            NSLog(@"int to str = %@", str1);

            

            // 字符串分割

            NSString *splitStr = @"test1;test2;test3;test4";

            NSArray *array = [splitStr componentsSeparatedByString:@";"];

            NSLog(@"%@", array);

            

            // 可变字符串

            NSMutableString *mulStr = [[NSMutableString alloc] initWithCapacity:1];

            [mulStr appendString:@"test1"];

            [mulStr appendFormat:@"test2"];

            [mulStr insertString:@";" atIndex:5];

            [mulStr replaceCharactersInRange:NSMakeRange(0, 4) withString:@"replace test"];

            [mulStr deleteCharactersInRange:NSMakeRange(0, 4)];

            

            // 查找字符串包含

            NSRange range = [mulStr rangeOfString:@"test"];

            NSLog(@"location : %ld", range.location);

            

            NSLog(@"mulStr is : %@", mulStr);

           // 字符串比较  小于:-1 等于:0 大于:1

        NSString *comStr1 = @"bbc";

        NSString *comStr2 = @"bbc";

        NSComparisonResult result = [comStr1 compare:comStr2];

        NSLog(@"result=%ld", result);

  • 相关阅读:
    【刷题】洛谷 P2764 最小路径覆盖问题
    【刷题】BZOJ 3546 [ONTAK2010]Life of the Party
    【刷题】BZOJ 3175 [Tjoi2013]攻击装置
    【刷题】BZOJ 4516 [Sdoi2016]生成魔咒
    【刷题】SPOJ 1811 LCS
    【刷题】洛谷 P3804 【模板】后缀自动机
    【刷题】SPOJ 8222 NSUBSTR
    (98)Wangdao.com_第三十天_拖拉事件
    ECMA Script 6_必须要知道的基础
    (97)Wangdao.com_第三十天_触摸事件
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4881703.html
Copyright © 2011-2022 走看看