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);
        
// 字符数组 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;
}

猜你喜欢

转载自www.cnblogs.com/zhangqing979797/p/13185466.html