//
// main.m
// NSString
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <Foundation/Foundation.h>
#pragma mark 創建字符串
void testCreat(){
NSString *str=@"你好。";
NSLog(@"str :%@",str);
NSString *str2=[NSString stringWithUTF8String:"哈哈哈"];
NSLog(@"str2 :%@",str2);
NSString *str3=[[NSString alloc] initWithString:@"測試3"] ;
NSLog(@"str3 :%@",str3);
}
#pragma mark 字符串的搜索
void search(){
NSString *str=@"123456456.txt";
NSLog(@"是否以 123 開頭:%i",[str hasPrefix:@"123"]);
NSLog(@"是否以 txt 結尾:%i",[str hasSuffix:@"txt"]);
//從前向后搜索
NSRange range=[str rangeOfString:@"456"];
if (range.location==NSNotFound) {
NSLog(@"未找到");
}else{
NSLog(@"找到的范圍是:%@",NSStringFromRange(range));
}
//從后向前搜索
range=[str rangeOfString:@"456" options:NSBackwardsSearch ];
NSLog(@"找到的范圍是:%@",NSStringFromRange(range));
}
#pragma mark 字符串截取
void mysubString(){
NSString *str=@"123456789";
//從第6個位置開始截取
NSLog(@"%@",[str substringFromIndex:6]);
//截取到第6個位置
NSLog(@"%@",[str substringToIndex:6]);
//隨便截取一段字符串
NSLog(@"%@",[str substringWithRange:NSMakeRange(1, 3)]);
NSString *str2=@"1-2-a-b-c";
//字符串分割
NSArray *array=[str2 componentsSeparatedByString:@"-"];
NSLog(@"%@",array);
}
#pragma mark 與路徑相關
void stringWithPath(){
//快速創建一個自動釋放的數組
NSMutableArray *array=[NSMutableArray array];
[array addObject:@"User"];
[array addObject:@"Wildcat"];
[array addObject:@"APP"];
//通過一個數組創建一個路徑字符串
NSString *path=[NSString pathWithComponents:array];
NSLog(@"Path is:%@",path);
//把一個路徑截取為數組
NSArray *apath=[path pathComponents];
NSLog(@"Array is;%@",apath);
//刪除最后一個目錄
NSLog(@"Array is;%@",[path stringByDeletingLastPathComponent]);
//拼接一個路徑
NSLog(@"Array is;%@",[path stringByAppendingPathComponent:@"123.txt"]);
}
#pragma mark 其他用法
void other(){
NSString *str=@"123";
int a=[str intValue];//轉化為整數
NSLog(@"%i",a);
//計算字數而不是字符數
NSLog(@"length=%zi",[@"我是字符串12345" length]);
//返回對應的字符
unichar c=[@"abc123" characterAtIndex:1];
NSLog(@"第2個字符是:%c",c);
//返回C語言字符串
const char *s=[@"abc" UTF8String];
NSLog(@"C語言字符串是:%s",s);
}
#pragma mark 主函數
int main(int argc, const char * argv[])
{
@autoreleasepool {
// testCreat();
// creatWithContent();
//writeToFile();
//search();
//調用截取字符串函數
//mysubString();
//stringWithPath();
other();
}
return 0;
}




