iOS 艾欧艾斯iOS开发实战iOS入门

iOS开发,搞定NSString

2016-09-26  本文已影响950人  林夕不昔

一、初始化方法

NSString *string1 = @"string";
NSString *str = [[NSString alloc] init];
 str = @"nullToFull";
char *c = "string";
 NSString *sring3 = [[NSString alloc] initWithCString:c encoding:nil];
NSString *str2 = @"I am a";
NSString *string2 = [NSString stringWithFormat:@"%@ string",str2];
NSString *path = [[NSBundlemainBundle] pathForResource:@"testString"ofType:@"text"];
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];

二、数据类型转换

    NSString *testStr = @"123456";
    //字符串转换成布尔类型
    BOOL boolValue = [testStr boolValue];
    NSLog(@"字符串转BOOL->%d",boolValue);
    
    //字符串转换成整型
    int intValue = [testStr intValue];
    NSLog(@"字符串转int->%d",intValue);
    
    //字符串转换成单精度 双精度类型
    float floatValue = [testStr floatValue];
    NSLog(@"字符串转单精度->%f",floatValue);
    
    double doubleValue = [testStr doubleValue];
    NSLog(@"字符串转双精度->%f",doubleValue);
    
    //其他类型转换成NSString
    NSString *stringOfValue = [NSString stringWithFormat:@"%d %f",intValue,floatValue];
    NSLog(@"其他类型转字符串->%@",stringOfValue);

三、字符串的一些简单操作

NSUInteger length = [string4 length];
    unichar index_char = [string1 characterAtIndex:0];
    NSString *subStr1 = [string1 substringFromIndex:1];
    NSString *subStr2 = [string2 substringToIndex:3];
    NSRange range = NSMakeRange(1, 2);
    NSString *subStr3 = [string4 substringWithRange:range];
    NSRange range2 = [string4 rangeOfString:@"a"];
    NSLog(@"字符“a”所在的位置->%ld",range2.location);
   //判断字符串是否为空
+(BOOL)stringIsEmpty:(NSString*)value
{
    BOOL isEmpty;
    if (value == nil || value.length == 0 || [value isKindOfClass:[NSNull class]])
    {
        isEmpty = YES;
    }
    else if ([value isKindOfClass:[NSString class]])
    {
        //自己定义一个NSCharacterSet, 包含需要去除的特殊符号
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        //字符串中去除特殊符号(在这里是空格)
        NSString *str = [value stringByTrimmingCharactersInSet:set];
        if ([str length] == 0)
        {
            isEmpty = YES;
        }
        else
        {
            isEmpty = NO;
        }
    }
    else
    {
        NSLog(@"string类型异常 %@", value);
    }
    return isEmpty;
}
    if ([string1 hasSuffix:@"c"])
    {
        NSLog(@"string1以字母“c”结尾");
    }
    if ([string1 hasPrefix:@"a"])
    {
        NSLog(@"string1以字母”a“开头");
    }
    if ([string1 isEqualToString:string2])
    {
        NSLog(@"string1和string2相等");
    }

四、较复杂操作

    NSString *stringBig = [NSString stringWithFormat:@"%@ %@ %@ %@",@"aaa",@"bbb",@"ccc",@"ddd"];
    NSString *stringAdd = [string1 stringByAppendingString:@".com"];
    NSRange range3 = NSMakeRange(4, 1);
    NSString *string5 = [string2 stringByReplacingCharactersInRange:range withString:@"abc"];
    NSString *newStr = [string1 stringByReplacingOccurrencesOfString:@"ab" withString:@"xyz"];
    //注意:分割字符串后的结果要用数组接收
    NSString *string4 = @"Hello abc! I am a string";
    NSArray *array = [string4 componentsSeparatedByString:@"a"];
    NSLog(@"分割字符串得到的数组-->%@",array);

输出结果为:

(
    "Hello ",
    "bc! I ",
    "m ",
    " string"
)
上一篇 下一篇

猜你喜欢

热点阅读