OC基础 字符串

// insert code here...
        NSLog(@"Hello, World!");
        //字符串查找子字符串在字符串中的范围
        NSString* str=@"http://ww.sxt.cn?user=admin&passwd=123456";
        NSRange range = [str rangeOfString:@"user="];
        //展示 location :开始位置  length: 范围长度
        NSLog(@"location=%lu,length=%lu",range.location,range.length);
        //判断字符串是不是以子字符串未为开始
        if([str hasPrefix:@"http://"])
        {
            NSLog(@"http协议");
            
        }
        // 判断是不是结束
        NSString* file=@"rock.mp4";
        if([file hasSuffix:@"mp3"])
        {
            NSLog(@"这是一个音乐文件");
            
        }
        // 字符串的位置和长度
        NSRange range1=[str rangeOfString:@"user="];
        NSRange range2=[str rangeOfString:@"&passwd"];
        NSLog(@"%lu,%lu,%lu,%lu",range1.location,range1.length,range2.location,range2.length);
        // 创建结构体
        NSUInteger userLength=range2.location-(range1.location+range1.length);
        NSUInteger userLocation=range1.location+range1.length;
        // 字符串截取 
        NSRange userRange={userLocation,userLength};
        NSString* user=[str substringWithRange:userRange];
        NSLog(@"user=%@",user);

    

        // 获取字符串对象的长度


        NSLog(@"length=%lu",str.length);


        NSRange range3=[str rangeOfString:@"passwd="];


        //从某个位置一直截取到字符串对象的末尾


        


        NSString* pwd=[str substringFromIndex:range3.location+range3.length];


        NSLog(@"pwd=%@",pwd);


        // 从左往右截取任意字符


        NSString* protocol = [str substringToIndex:7];


        NSLog(@"protocol=%@",protocol);


        


// 字符数组 char name【】
//缺点 长度不好确认
// 字符指针 char* name
//a 指向常量字符串 char* name=‘hellow’; 通过指针无法修改内容 需要改变指针指向
//b 指向字符数组  char str【10】=‘hellow’ char* name=str
// strcpy(str ,“world”);
//puts(name); 内容受制于人
// c 指向动态分配的自字符数组
//char* str=(char*)malloc(10*sizeof(char));
//strcpy(str,hellow)
//char * name=str
//strcpy(str,‘world’ );
//puts(name)//内容受制于人

// c语言字符操作函数
// strlen 长度  strcpy 复制字符串  strncpy 只复制制定长度 strcat 字符串拼接 strcmp //字符串比较 strncmp 只比较n个字符 strstr 查找子字符串 strchr 查找某个字符


// oc字符串分为两类 1.Nsstring ,创建了内容不在修改。2.比可变的字符串 NSMutableString
//他是NSString的子类




// try catch 每次都执行finally
//@try{
//    [object area]
//}
//@catch{NSException* e}
//{
//    NSLog()
//
//}
//@finally
//{
//    NSLog(
//
//}
//








int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        //NSString 创建
        //1.最简单的方式(指向了一个常量字符串)
        NSString* str=@"hello";
        //2.NSString的实例创建
        NSString* str2=[[NSString alloc]initWithString:@"world"];
   
        // 也可以指向常量字符串
        NSString* str3=[NSString stringWithString:str];
        //类方法
        
        NSLog(@"%@",str3);
        //3.格式化的方式 类方法
        NSString* str4=[NSString stringWithFormat:@"str%i",3];
        NSLog(@"%@",str4);
        //4.c语言字符串
        NSString* str5=[NSString stringWithUTF8String:"hellow"];
        NSLog(@"%@",str5);
        
        NSString* error=nil;
        //5.通过文件创建字符串
//        NSString* str6=[NSString stringWithContentsOfFile:<#(nonnull NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing  _Nullable * _Nullable)#>]
//
        //6.通过网页形式创建
        //url : http://www.baidu.com
 
        error=nil;
        NSURL* url=[NSURL URLWithString:@"http://www.baidu.com"];
        NSString* str7=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
        if(error)
        {
            NSLog(@"%@",error);
            
        }
        NSLog(@"%@",str7);
    }
    return 0;
}

字符串转换

// insert code here...
        NSLog(@"Hello, World!");
        // 字符串的转换  integerValue str to int
        NSString* str=@"123";
        int i =[str integerValue];
        NSLog(@"%i",i);
        // doubleValue str to double
        NSString* str3=@"1.23456";
        double d =[str3 doubleValue];
        NSLog(@"%f",d);
        // 字符串转换c语言字符串
        char* cstr=[str3 UTF8String];
        NSLog(@"%s",cstr);
        // 字符的大小写转换
        NSString* str4=@"adcdefg";
        NSString* upper=[str4 uppercaseString];
        NSLog(@"%@",upper);
        NSString* lower=[str4 lowercaseString];
        NSLog(@"%@",lower);
        

字符串的转换

//字符串比较 isEqualToString 是否一致
        NSString* str01=@"hello";
        NSString* str02=@"hello";
        if([str01 isEqualToString:str02]==YES)
        {NSLog(@"str1 等于 str2");
            
        // 比较字符串的大小
        NSComparisonResult result=[str01 compare:str02];
        if(result==NSOrderedSame)
        {
            NSLog(@"str1==str2");

        }
        else if(result ==NSOrderedAscending)
        {NSLog(@"str1<str2");}
        else{
            NSLog(@"str1>str2");

        }
        // 可变的字符串
        // 1.可变字符串的创建
        NSMutableString* str=[[NSMutableString alloc] initWithCapacity:20];
            
            // capacity(容量)在创建时给定的一个值,但不是这个字符串只能容纳那么多的字符,他在使用时可以根据需要增加空间
            
            //appendstring 在尾部添加字符串
        [str appendString:@"hello"];
        [str appendString:@"world"];
        NSLog(@"%@",str);
        // 2.2 //appendFormat 在尾部添加格式化字符串
        [str appendFormat:@"%i",100];
        NSLog(@"%@",str);
        // 2.3 插入字符串
            [str insertString:@"nihao" atIndex:5];
            NSLog(@"%@",str);
        //2.4 替换字符串
            NSRange range=NSMakeRange(6,6);
            [str replaceCharactersInRange:range withString:@" grate"];
            NSLog(@"%@",str);
            
        
            
原文地址:https://www.cnblogs.com/zhangqing979797/p/13185466.html