码迷,mamicode.com
首页 > 其他好文 > 详细

字符串

时间:2015-08-13 22:04:26      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

字符串
1.比较两个字符创是否相等if([Str1 isEquarToString:str2])
2.比较两个字符串是否为同一个对象if([Str1 isEquarToString:str2]) if(Str1==str2)
3.比较字符串的大小NSComparisonResult result=[string1 caseInsensitiveCompare:string2] 
4.字符串的长度[string length]
5.字符串的转换 
           大写:[string uppercaseString]
                          小写:[string lowercaseString]
                          首字母大写:[string capitalizedString]
           转换浮点型:[string floatValue]

6.字符串的截取
从Index位截取到末位[string substringFromIndex:Index]
从首位截取到Index位[string substringToIndex:Index]

截取特定范围
        rang.location=int
        rang.length=int
[string substringWithRange:rang]
                              ||
[string substringWithRange:NSMakeRange(2, 3)]
7.字符串的拼接
将字符串2拼接到字符串1尾端
[string1 stringByAppendingString:string2]
将将字符串2拼接到字符串1特定位置
[string1 stringByAppendingFormat:@"%@",string2]
8.切割字符串componentsSeparatedByString
[string componentsSeparatedByString:@“按格式切割.如空格,字符”]
9.字符串的查找[string rangeOfString:@“字符串数值-必须是连续”]
 如:       
NSString*string21=@"abcdffe-===fefjfwfw";
        NSRange range1=[string21 rangeOfString:@"abcd"];
        NSLog(@"%@",NSStringFromRange(range1));
        if(range1.location != NSNotFound)
        { NSLog(@"found!"); }


        //补充
        NSString*str1=@"123456456.txt";
        NSLog(@"是否以 123 开头:%i",[str1 hasPrefix:@"123"]);
        NSLog(@"是否以 txt 结尾:%i",[str1 hasSuffix:@"text"]);
        //从前向后搜索
        NSRange rang1=[str1 rangeOfString:@"456"];
        if (rang1.location==NSNotFound) {
            NSLog(@"未找到");
        }else{
            NSLog(@"找到的范围是:%@",NSStringFromRange(rang1));
            
        }
        //从后向前搜索
        rang1=[str1 rangeOfString:@"456" options:NSBackwardsSearch ];
        NSLog(@"找到的范围是:%@",NSStringFromRange(rang1));
        
        
    }
10.插入字符串[MutableString insertString:@“插入字符串内容” atIndex:从某位置插入]
11.替换字符串[MutableString replaceCharactersInRange:NSMakeRange(范围) withString:@“替换字符串内容”]
12.删除字符串[MutableString deleteCharactersInRange:NSMakeRange(范围)]
13.添加字符串[MutableString stringByAppendingString:@“所添加字符串内容”]
14.字符串路径
//快速创建一个自动释放的数组
    NSMutableArray *array1=[NSMutableArray array];
    [array1 addObject:@"User"];
    [array1 addObject:@"Wildcat"];
    [array1 addObject:@"APP"];
    //通过一个数组创建一个路径字符串
    NSString *path=[NSString pathWithComponents:array1];
    NSLog(@"Path is:%@",path);//Path is:User/Wildcat/APP
    //把一个路径截取为数组
    NSArray *apath=[path pathComponents];
    NSLog(@"Array is;%@",apath);
    
    //删除最后一个目录
    
    NSLog(@"Array1 is;%@",[path stringByDeletingLastPathComponent]);
    //拼接一个路径
    NSLog(@"Array1 is;%@",[path stringByAppendingPathComponent:@"123.txt"]);

15.#pragma mark 其他用法
    NSString *str7=@"123";
    int a=[str7 intValue];//转化为整数
    NSLog(@"%i",a);
    //计算字数而不是字符数
    NSLog(@"length=%zi",[@"我是字符串12345" length]);
    //返回对应的字符
    unichar c=[@"abc123" characterAtIndex:1];
    NSLog(@"第2个字符是:%c",c);
    
    //返回C语言字符串
    const char *s=[@"abc" UTF8String];
    NSLog(@"C语言字符串是:%s",s);

 

字符串

标签:

原文地址:http://www.cnblogs.com/sunbinboke/p/4728313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!