• 【Foundation Frame】NSString


    NSString代表字符序列不可变的字符串,功能更强大,更方便简单。
    下面代码列举了大部分常用的NSString方法。

    //
    //  main.m
    //  OcString
    //
    //  Created by mac on 14-12-2.
    //  Copyright (c) 2014 mac. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    void dynamicCreateString()
    {

       
    //1
       
    NSString *str1 = @"neusoft i love you";
       
    NSLog(@"%@",str1);
       
    //2
       
    NSString *str2 = [[NSString alloc]init];
        [str2
    release];
       
    //使用oc字符串付初值
       
    NSString *str3 = [[NSString alloc]initWithString:str1];
       
    NSLog(@"%@",str3);
        [str3
    release];
       
    //使用c语言的字符串初始化
       
    NSString *str4 = [[NSString alloc] initWithUTF8String:"I love you"];
       
    NSLog(@"%@",str4);
        [str4
    release];
       
    //格式化创建方式
       
    NSString *str5 = [[NSString alloc]initWithFormat:@"i am %i years old",10];
       
    NSLog(@"%p, %@",str5,str5);
       
    //[str5 release];
       
       
    NSString *str6 =  [str5 stringByAppendingString:str1];
       
    NSLog(@"%p, %@",str6,str6);
        [str5
    release];
        [str6
    release];
       
       
    //使用文件初始化
       
    NSError * error;
       
    NSString *str7 = [[NSString alloc]initWithContentsOfFile:@"/Users/mac/Desktop/a.txt" encoding:NSUTF8StringEncoding error:&error];
       
    NSLog(@"%@, %@",str7,error);
       
       
    NSString * path = @"file:///Users/mac/Desktop/a.txt";
       
    NSString * httpContents = @"http://people.com.cn";

       
       
    NSURL * url = [NSURL URLWithString:httpContents];
       
    NSString *str8 = [[NSString alloc]initWithContentsOfURL:url encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000) error:&error];
        NSLog(
    @"%@, %@",str8,error);
        [str8
    release];
       
       
    }

    void staticCreateString()
    {
       
    NSString * str1 = [NSString stringWithFormat:@"i am %i years old",10];
       
    NSLog(@"%@",str1);
    }

    #pragma mark  导出到文件中

    void export()
    {
       
       
    //向文件中写入,持久化的过程
       
    NSString * str = @"i was a child , i have a dreamer ";
       
    NSString * path = @"file:///Users/mac/Desktop/a.txt";

       
    NSError *error;
        [str
    writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
       
       
    NSLog(@"%@",error);
       
       
    NSURL * url = [NSURL URLWithString:path];
        [str
    writeToURL:url atomically: YES encoding:NSUTF8StringEncoding error:&error];
      
    NSLog(@"%@",error);
       
    }


    void otherMethord()
    {
       
    //请求字符串长度
       
    NSLog(@"%lu",[@"abcd吴剑光" length]);
       
    NSLog(@"%c",[@"abcd吴剑光" characterAtIndex:3]);
       
    }


    void caseMethord()
    {
       
    NSString * str = @"I'am  boY ,i have a dream";
       
       
    NSLog(@"%@",[str uppercaseString]);
       
    NSLog(@"%@",[str lowercaseString]);
       
    NSLog(@"%@",[str capitalizedString]);
       
    }


    void campareWithString()
    {
       
    NSString * str = @"Z'am  boY ,i have a dream";
       
    NSString * str1 = @"I'am  boY ,i have a dream";
       
       
    if ([str isEqualToString:str1]) {
           
    NSLog(@"true");
        }
       
       
    NSLog(@"%li",[str compare:str1]);
       
    }

    void searchString()
    {
       
    NSString * str = @"neuZ'am  boY ,i have a dream";
       
    NSLog(@"%i",[str hasPrefix:@"neu"]);
       
    NSLog(@"%i",[str hasSuffix:@"exm"]);
       
       
    NSRange range1 = [str rangeOfString:@"have"];
       
    NSLog(@"%lu,%lu",range1.location,range1.length);
       
       
       
    NSRange range2 = [str rangeOfString:@"have" options:NSBackwardsSearch];
       
    NSLog(@"%lu,%lu",range2.location,range2.length);
       
    }

    void subString()
    {
       
    NSString * str = @"neuZ'am #boY #i have# a dream";
       
    NSLog(@"%@", [str substringFromIndex:3]);
       
    NSLog(@"%@", [str substringToIndex:3]);
       
    NSLog(@"%@",  [str substringWithRange:NSMakeRange(3, 5)]);
       
    NSArray * array = [str componentsSeparatedByString:@"#"];
       
    NSLog(@"%@",array);
       
       
    }

    void pathString()
    {
       
       
    NSString * str = @"neuZ'am #boY #i have# a dream";
       
    NSArray * array = [str componentsSeparatedByString:@"#"];
       
    NSLog(@"%@", [NSString pathWithComponents:array]);
       
       
    NSString * path = @"Users/mac/desktop";
       
    NSLog(@"%@", [path pathComponents]);
       
       
    NSLog(@"%i", [path isAbsolutePath]);
       
       
    NSLog(@"%@", [path stringByAppendingPathComponent:@"abc"]);
       
       
    NSLog(@"%@", path);
       
    }

    void extentionString()
    {
       
    NSString * path = @"/Users/mac/Desktop/a.txt";
       
    NSLog(@"%@", [path pathExtension]);
       
    NSLog(@"%@", [path stringByDeletingPathExtension]);
       
    NSLog(@"%@", [path stringByAppendingPathExtension:@"xml"]);
    }

    void exchangeType()
    {
       
    NSString *str1 = @"234.2342424";
       
    NSString *str2 = @"234.45";
       
    NSString *str3 = @"234";
       
    NSString *str4 = @"abcde";
       
       
    NSLog(@"%f",[str1 doubleValue]);
       
    NSLog(@"%f",[str2 floatValue]);
       
    NSLog(@"%i",[str3 intValue]);
       
    NSLog(@"%s",[str4 UTF8String]);
       
    }

    #pragma mark NSMutableString
    void mutableStringOperation()
    {
       
    NSMutableString * str = [NSMutableString stringWithCapacity:10];
        [str
    setString:@"i was girl"];
       
    NSLog(@"%@", str);
        [str
    appendFormat:@" %i years old", 10];
       
    NSLog(@"%@", str);
        [str
    replaceCharactersInRange:NSMakeRange(2, 3) withString:@"abcdekfslfj"];
       
    NSLog(@"%@", str);


    }
    int main(int argc, const char * argv[]) {
       
       
    @autoreleasepool {
          
           
    mutableStringOperation();
           
           
    exchangeType();
           
    extentionString();
           
           
    pathString();
           
    subString();
           
    searchString();
           
    dynamicCreateString();
           
    staticCreateString();
           
           
    export() ;
           
           
    otherMethord();
           
    caseMethord();
           
           
    campareWithString();
        }
       
    return 0;
    }
  • 相关阅读:
    自定义注解!绝对是程序员装逼的利器!!
    我女儿说要看雪,但是我家在南方,于是我默默的拿起了键盘,下雪咯。
    零基础转行前端,如何拿下了字节跳动、美团等大厂offer
    “TensorFlow 开发者出道计划”全攻略,玩转社区看这里!
    web面试必问的题
    Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    520了,用32做个简单的小程序
    虚拟机找不到本机vmnet0,vmnet8,无法连接xshell,解决方案
    Linux用户登出之后保持后台进程(nohup)
    Debian取消从光盘安装软件的方式(please insert the disc labeled)
  • 原文地址:https://www.cnblogs.com/shujucn/p/7481460.html
Copyright © 2020-2023  润新知