【Foundation Frame】NSString

NSString代表字符序列不可变的字符串,功能更强大,更方便简单。
下面代码列举了大部分常用的NSString方法。

//
//  main.m
//  OcString
//
//  Created by mac on 14-12-2.
//  Copyright (c) 2014 mac. All rights reserved.
//

#import <Foundation/Foundation.h>

void dynamicCreateString()
{

   
//1
   
NSString *str1 = @"neusoft i love you";
   
NSLog(@"%@",str1);
   
//2
   
NSString *str2 = [[NSString alloc]init];
    [str2
release];
   
//使用oc字符串付初值
   
NSString *str3 = [[NSString alloc]initWithString:str1];
   
NSLog(@"%@",str3);
    [str3
release];
   
//使用c语言的字符串初始化
   
NSString *str4 = [[NSString alloc] initWithUTF8String:"I love you"];
   
NSLog(@"%@",str4);
    [str4
release];
   
//格式化创建方式
   
NSString *str5 = [[NSString alloc]initWithFormat:@"i am %i years old",10];
   
NSLog(@"%p, %@",str5,str5);
   
//[str5 release];
   
   
NSString *str6 =  [str5 stringByAppendingString:str1];
   
NSLog(@"%p, %@",str6,str6);
    [str5
release];
    [str6
release];
   
   
//使用文件初始化
   
NSError * error;
   
NSString *str7 = [[NSString alloc]initWithContentsOfFile:@"/Users/mac/Desktop/a.txt" encoding:NSUTF8StringEncoding error:&error];
   
NSLog(@"%@, %@",str7,error);
   
   
NSString * path = @"file:///Users/mac/Desktop/a.txt";
   
NSString * httpContents = @"http://people.com.cn";

   
   
NSURL * url = [NSURL URLWithString:httpContents];
   
NSString *str8 = [[NSString alloc]initWithContentsOfURL:url encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000) error:&error];
    NSLog(
@"%@, %@",str8,error);
    [str8
release];
   
   
}

void staticCreateString()
{
   
NSString * str1 = [NSString stringWithFormat:@"i am %i years old",10];
   
NSLog(@"%@",str1);
}

#pragma mark  导出到文件中

void export()
{
   
   
//向文件中写入,持久化的过程
   
NSString * str = @"i was a child , i have a dreamer ";
   
NSString * path = @"file:///Users/mac/Desktop/a.txt";

   
NSError *error;
    [str
writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
   
   
NSLog(@"%@",error);
   
   
NSURL * url = [NSURL URLWithString:path];
    [str
writeToURL:url atomically: YES encoding:NSUTF8StringEncoding error:&error];
  
NSLog(@"%@",error);
   
}


void otherMethord()
{
   
//请求字符串长度
   
NSLog(@"%lu",[@"abcd吴剑光" length]);
   
NSLog(@"%c",[@"abcd吴剑光" characterAtIndex:3]);
   
}


void caseMethord()
{
   
NSString * str = @"I'am  boY ,i have a dream";
   
   
NSLog(@"%@",[str uppercaseString]);
   
NSLog(@"%@",[str lowercaseString]);
   
NSLog(@"%@",[str capitalizedString]);
   
}


void campareWithString()
{
   
NSString * str = @"Z'am  boY ,i have a dream";
   
NSString * str1 = @"I'am  boY ,i have a dream";
   
   
if ([str isEqualToString:str1]) {
       
NSLog(@"true");
    }
   
   
NSLog(@"%li",[str compare:str1]);
   
}

void searchString()
{
   
NSString * str = @"neuZ'am  boY ,i have a dream";
   
NSLog(@"%i",[str hasPrefix:@"neu"]);
   
NSLog(@"%i",[str hasSuffix:@"exm"]);
   
   
NSRange range1 = [str rangeOfString:@"have"];
   
NSLog(@"%lu,%lu",range1.location,range1.length);
   
   
   
NSRange range2 = [str rangeOfString:@"have" options:NSBackwardsSearch];
   
NSLog(@"%lu,%lu",range2.location,range2.length);
   
}

void subString()
{
   
NSString * str = @"neuZ'am #boY #i have# a dream";
   
NSLog(@"%@", [str substringFromIndex:3]);
   
NSLog(@"%@", [str substringToIndex:3]);
   
NSLog(@"%@",  [str substringWithRange:NSMakeRange(3, 5)]);
   
NSArray * array = [str componentsSeparatedByString:@"#"];
   
NSLog(@"%@",array);
   
   
}

void pathString()
{
   
   
NSString * str = @"neuZ'am #boY #i have# a dream";
   
NSArray * array = [str componentsSeparatedByString:@"#"];
   
NSLog(@"%@", [NSString pathWithComponents:array]);
   
   
NSString * path = @"Users/mac/desktop";
   
NSLog(@"%@", [path pathComponents]);
   
   
NSLog(@"%i", [path isAbsolutePath]);
   
   
NSLog(@"%@", [path stringByAppendingPathComponent:@"abc"]);
   
   
NSLog(@"%@", path);
   
}

void extentionString()
{
   
NSString * path = @"/Users/mac/Desktop/a.txt";
   
NSLog(@"%@", [path pathExtension]);
   
NSLog(@"%@", [path stringByDeletingPathExtension]);
   
NSLog(@"%@", [path stringByAppendingPathExtension:@"xml"]);
}

void exchangeType()
{
   
NSString *str1 = @"234.2342424";
   
NSString *str2 = @"234.45";
   
NSString *str3 = @"234";
   
NSString *str4 = @"abcde";
   
   
NSLog(@"%f",[str1 doubleValue]);
   
NSLog(@"%f",[str2 floatValue]);
   
NSLog(@"%i",[str3 intValue]);
   
NSLog(@"%s",[str4 UTF8String]);
   
}

#pragma mark NSMutableString
void mutableStringOperation()
{
   
NSMutableString * str = [NSMutableString stringWithCapacity:10];
    [str
setString:@"i was girl"];
   
NSLog(@"%@", str);
    [str
appendFormat:@" %i years old", 10];
   
NSLog(@"%@", str);
    [str
replaceCharactersInRange:NSMakeRange(2, 3) withString:@"abcdekfslfj"];
   
NSLog(@"%@", str);


}
int main(int argc, const char * argv[]) {
   
   
@autoreleasepool {
      
       
mutableStringOperation();
       
       
exchangeType();
       
extentionString();
       
       
pathString();
       
subString();
       
searchString();
       
dynamicCreateString();
       
staticCreateString();
       
       
export() ;
       
       
otherMethord();
       
caseMethord();
       
       
campareWithString();
    }
   
return 0;
}
posted @ 2014-12-03 19:23  shujucn  阅读(112)  评论(0编辑  收藏  举报