OC-字符串操作

//  Created by huanghaiyan on 14-08-07.

//  Copyright (c) 2014年 huanghaiyan. All rights reserved.

#import <Foundation/Foundation.h>

#include <string.h>

int main(int argc,const char * argv[])

{

//字符串操作

NSString *string = @”hello,world!”;

NSLog(@”%@”,string);

NSString *formatStr = [NSStringstringWithFormat:@”I am zhang%@, I am %d years old!”,@”san”,15];

NSLog(@”%@”,formatStr);

NSString *formatStr1 = [NSStringstringWithFormat:@”I am zhang%d, I am %d years old!”,3,15];

NSLog(@”%@”,formatStr1);

//通过C字符串来创建OC的字符串

NSString *formCStr = [NSStringstringWithCString:”hello,world”encoding:NSUTF8StringEncoding];

NSLog(@”formCStr:%@”,formCStr);

//将OC的字符串对象转换成C的字符串

const char  *ptr = [formCStrUTF8String];

printf(“===>%s\n”,ptr);

//获取字符串长度

NSUInteger length = [formatStr length];

NSLog(@”formatStr’s length is :%lu”,length);

//c语言字符串比较

char *str =”hello,world!”;

char arr[] = “Hello,world!”;

if(strcasecmp(str, arr) ==0)

{

NSLog(@”str is equal to arr”);

}else{

NSLog(@”str is not equal arr”);

}

//oc 字符串比较

NSString *string1 =@”This is String1″;

NSString *string2 =@”This is string2″;

BOOL result01 = [string1 isEqualToString:string2];

NSLog(@”result01 :%d”,result01);

//compare (返回三种值)

NSComparisonResult result02 = [string1 compare:string2];

NSLog(@”result02 :%ld”,result02);//结果为-1表示左值比右值小

//不区分大小写比较1

if([string1caseInsensitiveCompare:string2] == NSOrderedSame){

NSLog(@”string1 is equal to string2″);

}else {

NSLog(@”not equal”);

}

//不区分大小写比较2

NSComparisonResult result03 = [string1compare:string2 options:NSCaseInsensitiveSearch |NSNumericSearch];

if (result03 == NSOrderedSame) {

NSLog(@”string1 is equal to string2″);

}elseif(result03 == NSOrderedSame){

NSLog(@”string1 is little!”);

}else{

NSLog(@”string2 is little!”);

}

//改变字符串的大小写

NSString *string3 = @”Astring”;

NSString *string4 = @”String”;

NSLog(@”after uppercase string3:%@”,[string3uppercaseString]);

NSLog(@”after lowercase string4:%@”,[string4lowercaseString]);

//字符串是否含有后缀

NSString *filename = @”chaper.txt”;

if([filename hasSuffix:@”.txt”])

{

NSLog(@”It’s a txt file”);

}

//字符串是否含有前缀

NSString *fileName2 =@”draft-hello.pages”;

if([fileName2 hasPrefix:@”draft”])

{

NSLog(@”It’s a draft”);

}

//取出指定字符串中子串的范围

NSString *string5 =@”hello world,hello kitty!”;

NSString *string6 = @”kitty”;

NSRange range = [string5 rangeOfString:string6];

NSString *rangeStr = [NSStringstringWithFormat:@”Location is %lu,length is %lu”,range.location,range.length];

NSLog(@”range:%@”,rangeStr);

//提取子串(子串从哪个位置开始到最后)

NSString *string7 =@”This is a string”;

NSString *string8 = [string7 substringFromIndex:3]//截取string7从3开始到最后

//截取string7的后四位

NSString *string8 = [string7 substringFromIndex:string7 – 4];

NSLog(@”string8:%@”,string8);

//提取子串(从开始到指定的位置)

NSString *string9 = [string7 substringToIndex:3];

NSLog(@”string9:%@”,string9);

//提取子串(从哪个位置开始,提取多长个字符串)

NSString *string10 = [string7 substringWithRange:NSMakeRange(3,6)];

NSLog(@”string10:%@”,string10);

NSString *string11 = [string7 substringWithRange:NSMakeRange(3,0)];

NSLog(@”string10:%@”,string11);

//文件扩展名

NSString *fileName3 = @”~/hello.txt”;

NSLog(@”Extention:%@”,[fileName3pathExtension]);

//绝对路径和相对路径的转换

NSString *path = @”~/test.mou”;

NSString *absPath = [pathstringByExpandingTildeInPath];

NSLog(@”absPath:%@”,absPath);

NSLog(@”path:%@”,[absPathstringByAbbreviatingWithTildeInPath]);

return 0;

}

分享到: 更多
Separator image Posted in IOS.

发表评论

电子邮件地址不会被公开。 必填项已用*标注