Object-c 总结之NSString

参照《iphone SDK 3开发快速上手》编写,特此声明        

       //创建一个NSString

        NSString *myString1 = @"some string";

        NSLog(@"%@",myString1);

       

        //格式化创建

        NSString *myString2 = [NSString stringWithFormat:@"%@",myString1];

          NSLog(@"%@",myString2);

        //返回数字的值

        NSString *myString3 = @"1234abc";

        double doubleString = [myString3 floatValue];//获得123

          NSLog(@"%f",doubleString);

        int intString = [myString3 intValue];//获得123

        NSLog(@"%d",intString);

        NSUInteger stringLenth = [myString3 length];

        /*NSUinteger 的输出方式为%lu*/

        NSLog(@"%lu",stringLenth);

        //比较两个字符串,若相同返回TURE

        BOOL areEqual = 

        [myString1 isEqualToString:myString2];

        NSLog(@"areEqual = %d",areEqual);

        //区分大小写的比较

        BOOL areEqual1 = ([myString1 caseInsensitiveCompare:myString2] == NSOrderedSame);

        NSLog(@"areEqual1 = %d",areEqual1);

        //转换大小写

        NSString *myString4 = @"asdfg";

        NSString *upper = [myString4 uppercaseString];

        NSString *lower = [upper lowercaseString];

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

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

        

        //截去字符串(以空格为例)

        NSString *myString5 = @"   one     two three  ";

        NSString *trimmed1 = [myString5 stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];

        //whitespaceAndNewlineCharacterSet去除前后的空格和换行符

        NSString *trimmed2 = [myString5 stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];

        //whitespaceCharacterSet 去除前后的空格,实际效果来看只实现了去除首字母前面的空格

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

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

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

    //predicate 断言断定,使基于,下面的方法实现删除空格

        NSPredicate *noE = [NSPredicatepredicateWithFormat:@"SELF!=''"];

        NSArray *part = [myString5 componentsSeparatedByCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];

        NSArray *file = [part filteredArrayUsingPredicate:noE];

        NSString *trimmed3 = [file componentsJoinedByString:@""];

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

        

        //有已知字符串创建子字符串

        NSString * number = @"abx cdefghi gklmn";

        //substringToIndex获取字符串的前三个

        //substringFromIndex从第三个开始截取到最后

        NSString *myString6 = [number substringToIndex:3];

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

        

        NSRange range = NSMakeRange(4, 5);

        //用边界截取起始位置为第四个,长度为5个长度

        NSString *aString = [number substringWithRange:range];

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

        //创建成数组

        NSArray *arr = [number componentsSeparatedByString:@" "];

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

        //替换子字符串

        NSString *myString7 = [number stringByReplacingOccurrencesOfString:@"efg"withString:@"aaa"];

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

        //查找子字符串,这段代码返回(4,3;

        NSRange found = [number rangeOfString:@"cde"];

        NSLog(@"found.location = %lu",found.location);

        NSLog(@"%lu",found.length);

        //判读那是否包含

        BOOL found1 = ([number rangeOfString:@"cde"].location !=NSNotFound);

        NSLog(@"found1 = %lu",found1);

        //组合字符串

        NSString *myString9 = [myString1 stringByAppendingString:myString7];

        NSLog(@"myString9 = %@,myString1 = %@,myString7 = %@",myString9,myString1,myString7);

        

        NSString *myString10 = [myString1 stringByAppendingFormat:myString7];

         NSLog(@"myString10 = %@,myString1 = %@,myString7 = %@",myString10,myString1,myString7);

        

        //将文件内容写入到字符串中

        NSString *fileContents = [NSString stringWithContentsOfFile:@"123.text"];

        //获取文件扩展名

        NSString *filename = @"11111.txt";

        NSString *fileExtension = [filename pathExtension];

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

        

        //URL内容写入字符串,有点问题

        NSURL *url = [NSURLURLWithString:@"http://www.baidu.com"];

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

        NSString *pageContents  = [NSString stringWithContentsOfURL:url];

        //NSLog(@"pageContents = %@",pageContents);


你可能感兴趣的:(String,File,url,iPhone,扩展)