天天看点

OC中NSString方法的使用和理解NSString的创建字符串比较字符串搜索   字符串截取    字符串替换    字符串转换字符串大小写转换      字符串路径的判断

不积跬步,无以至千里;不积小流,无以成江海.

NSString的创建

创建常量字符串,注意使用“@“符号

NSString *astring = @"This is a String!";
           

创建空字符串,给予赋值。

NSString *s = [[NSString alloc] init];

s=@"quanzi";
           

创建格式化字符串:占位符(由一个%加一个字符组成)

[NSString stringWithFormat:@"quanzi%i岁了", 30];
           

字符串比较

    NSString *str1 = @"quanzi";

    NSString *str2 =@"quanzi";

    if (str1 == str2) {

        NSLog(@"字符串地址一样");

    }

    if ([str1 isEqualToString:str2]) {

        NSLog(@"字符串内容一样");

    }
           

(NSComparisonResult)compare:(NSString *)string

  • 这个方法可以用来比较两个字符串内容的大小
  • 比较方法: 逐个字符地进行比较ASCII值,返回NSComparisonResult作为比较结果
  • NSComparisonResult是一个枚举,有3个值:
    • 如果左侧> 右侧,返回NSOrderedDescending,
    • 如果左侧< 右侧,返回NSOrderedAscending,
    • 如果左侧== 右侧返回NSOrderedSame
    NSString *str1 = @"abc";

    NSString *str2 = @"abd";

    switch ([str1 compare:str2]) {

        case NSOrderedAscending:

            NSLog(@"后面一个字符串大于前面一个");

            break;

        case NSOrderedDescending:

            NSLog(@"后面一个字符串小于前面一个");

            break;

        case NSOrderedSame:

            NSLog(@"两个字符串一样");

            break;

    }
           

 输出结果: 后面一个字符串大于前面一个

(NSComparisonResult) caseInsensitiveCompare:(NSString *)string;

  • 忽略大小写进行比较,返回值与compare:一致
    NSString *str1 = @"abc";

    NSString *str2 = @"ABC";

    switch ([str1 caseInsensitiveCompare:str2]) {

        case NSOrderedAscending:

            NSLog(@"后面一个字符串大于前面一个");

            break;

        case NSOrderedDescending:

            NSLog(@"后面一个字符串小于前面一个");

            break;

        case NSOrderedSame:

            NSLog(@"两个字符串一样");

            break;

    }

    输出结果:两个字符串一样
           

字符串搜索

(BOOL)hasPrefix:(NSString *)aString;

 是否以aString开头

NSString *str1 = @"abcdef";

NSString *str2 =@"ab";

bool b = [str1 hasPrefix:str2];

NSLog(@"%i",b);
           

   (BOOL)hasSuffix:(NSString *)aString;

   是否以aString结尾 

  NSString *str3=@"ef";

  bool b2 = [str1 hasSuffix:str3];

  NSLog(@"%i",b2);
           

     (NSRange)rangeOfString:(NSString *)aString;

     1.用来检查字符串内容中是否包含了aString

     2.如果包含, 就返回aString的范围

     3.如果不包含, NSRange的location为NSNotFound, length为0

NSRange r =  [str1 rangeOfString:str2];

NSLog(@"长度是=%i,位置是=%i",r.length,r.location);//长度是=2,位置是=0
           

   字符串截取

    (NSString *)substringFromIndex:(NSUInteger)from;

    从指定位置from开始(包括指定位置的字符)到尾部

    NSString *str1 = @"fakjhfakjnfjahfj";

    str1 = [str1 substringFromIndex:7];

    NSLog(@"str = %@", str1); //kjnfjahfj
           

    (NSString *)substringToIndex:(NSUInteger)to;

    从字符串的开头一直截取到指定的位置to,但不包括该位置的字符

    NSString *str2 = @"fakjhfakjnfjahfj";

    str2 = [str2 substringToIndex:10];

    NSLog(@"str = %@", str2); //fakjhfakjn
           

    (NSString *)substringWithRange:(NSRange)range;

    按照所给出的NSRange从字符串中截取子串

    NSString *str = @"<head>quanzi</head>";

    NSRange range;

    range.location = [str rangeOfString:@">"].location + 1;

    range.length = [str rangeOfString:@"</"].location - range.location;

    NSString *res = [str substringWithRange:range];

    NSLog(@"res = %@", res);//quanzi
           

    字符串替换

    (NSString)stringByReplacingOccurrencesOfString:(NSString )target withString:(NSString *)replacement;

    用replacement替换target

    NSString *str1 = @"http:**quanzi.com*img*info";

    NSString *newStr1 = [str1 stringByReplacingOccurrencesOfString:@"*" withString:@"/"];

    NSLog(@"newStr = %@", newStr1);//newStr1 = http://quanzi.com/img/info
           

    (NSString )stringByTrimmingCharactersInSet:(NSCharacterSet )set;

    去除首尾

    NSString *str2 =  @"   http://quanzi.com/img/info   ";

    NSString *newStr2 = [str2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    NSLog(@"newStr =|%@|", newStr2);//newStr2 =|http://quanzi.com/img/info|

    NSString *str3 =  @"***http://quanzi.com/img/info***";

    NSString *newStr3 = [str3 stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*"]];

    NSLog(@"newStr =%@", newStr3);//newStr =http://quanzi.com/img/info
           

    字符串转换

    转为基本数据类型

    (double)doubleValue;

    (float)floatValue;

    (int)intValue;

NSString *str1 = @"110";

NSString *str2 = @"10";

int res = str1.intValue + str2.intValue;

NSLog(@"res = %i", res); //120

NSString *str1 = @"110";

NSString *str2 = @"10.1";

double res = str1.doubleValue + str2.doubleValue;

NSLog(@"res = %f", res);//120.100000

    

           

转为C语言中的字符串

(char *)UTF8String;

 NSString *str = @"abc";

const char *cStr = [str UTF8String];

NSLog(@"cStr = %s", cStr);//cStr = abc

char *cStr = "abc";

NSString *str = [NSString stringWithUTF8String:cStr];

NSLog(@"str = %@", str);//str = abc
           

字符串大小写转换    

全部字符转为大写字母

(NSString *)uppercaseString;

NSString *str = @"abc";

str = [str uppercaseString];

NSLog(@"str = %@", str);//str = ABC
           

  全部字符转为小写字母

  (NSString *)lowercaseString

NSString *str = @"ABC";

str = [str lowercaseString];

NSLog(@"str = %@", str);//str = abc
           

    首字母变大写,其他字母都变小写

    (NSString *)capitalizedString

    NSString *str = @"abdcfaDFJdfjh";

    str = [str capitalizedString];

    NSLog(@"str = %@", str);//str = Abdcfadfjdfjh
           

  字符串路径的判断

(BOOL)isAbsolutePath;

是否为绝对路径

//其实就是判断是否以/开头//
NSString *str = @"Users/quanzi/Desktop/info.txt";//不是绝对路径
if ([str isAbsolutePath]) {
        NSLog(@"是绝对路径");

}else
{
       NSLog(@"不是绝对路径");
}
           

(NSString *)lastPathComponent;

获得最后一个目录

// 截取最后一个/后面的内容

NSString *str = @"/Users/quanzi/Desktop/info.txt";

NSString *component = [str lastPathComponent];

NSLog(@"component = %@", component);//info.txt
           

(NSString *)stringByDeletingLastPathComponent;

删除最后一个目录

// 其实就是上次最后一个/和之后的内容

NSString *str = @"/Users/quanzi/Desktop/info.txt";

NSString *newStr = [str stringByDeletingLastPathComponent];

NSLog(@"newStr = %@", newStr);///Users/quanzi/Desktop
           

(NSString )stringByAppendingPathComponent:(NSString )str;

在路径的后面拼接一个目录 (也可以使用stringByAppendingString:或者stringByAppendingFormat:拼接字符串内容)

// 其实就是在最后面加上/和要拼接得内容

// 注意会判断后面有没有/有就不添加了, 没有就添加, 并且如果有多个会替换为1个

NSString *str = @"/Users/quanzi/Desktop/";

NSString *newStr = [str stringByAppendingPathComponent:@"info.txt"];

NSLog(@"newStr = %@", newStr);///Users/quanzi/Desktop/info.txt
           

 (NSString *)pathExtension;

获得拓展名

// 其实就是从最后面开始截取.之后的内容

NSString *str = @"info.txt";

NSString *str = @"abc.info.txt";

NSString *extension = [str pathExtension];

NSLog(@"extension = %@", extension);//extension = txt
           

(NSString *)stringByDeletingPathExtension;

删除尾部的拓展名

NSString *str = @"abc.info.txt";

NSString *newStr = [str stringByDeletingPathExtension];

 NSLog(@"newStr = %@", newStr);//newStr = abc.lnj
           

(NSString )stringByAppendingPathExtension:(NSString )str;

在尾部添加一个拓展名

// 其实就是在最后面拼接上.和指定的内容

NSString *str = @"info";

NSString *newStr = [str stringByAppendingPathExtension:@"gif"];

NSLog(@"newStr = %@", newStr);//newStr = info.gif
           

本文都是个人的一部分总结,以后都会不断的更新和总结.有什么写的不对的地方,欢迎大神指导.