首页 > 代码库 > 【Foundation Frame】NSString

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

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

void staticCreateString()
{
   
NSString * str1 = [NSStringstringWithFormat:@"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:pathatomically:YESencoding:NSUTF8StringEncodingerror:&error];
   
   
NSLog(@"%@",error);
   
   
NSURL * url = [NSURLURLWithString:path];
    [str
writeToURL:urlatomically:YESencoding:NSUTF8StringEncodingerror:&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(@"%@",[struppercaseString]);
   
NSLog(@"%@",[strlowercaseString]);
   
NSLog(@"%@",[strcapitalizedString]);
   
}


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

void searchString()
{
   
NSString * str =@"neuZ‘am  boY ,i have a dream";
   
NSLog(@"%i",[strhasPrefix:@"neu"]);
   
NSLog(@"%i",[strhasSuffix:@"exm"]);
   
   
NSRange range1 = [strrangeOfString:@"have"];
   
NSLog(@"%lu,%lu",range1.location,range1.length);
   
   
   
NSRange range2 = [strrangeOfString:@"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 = [strcomponentsSeparatedByString:@"#"];
   
NSLog(@"%@",array);
   
   
}

void pathString()
{
   
   
NSString * str =@"neuZ‘am #boY #i have# a dream";
   
NSArray * array = [strcomponentsSeparatedByString:@"#"];
   
NSLog(@"%@", [NSStringpathWithComponents: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",[str1doubleValue]);
   
NSLog(@"%f",[str2floatValue]);
   
NSLog(@"%i",[str3intValue]);
   
NSLog(@"%s",[str4UTF8String]);
   
}

#pragma mark NSMutableString
void mutableStringOperation()
{
   
NSMutableString * str = [NSMutableStringstringWithCapacity: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, constchar * argv[]) {
   
   
@autoreleasepool {
      
       
mutableStringOperation();
       
       
exchangeType();
       
extentionString();
       
       
pathString();
       
subString();
       
searchString();
       
dynamicCreateString();
       
staticCreateString();
       
       
export() ;
       
       
otherMethord();
       
caseMethord();
       
       
campareWithString();
    }
   
return0;
}

【Foundation Frame】NSString