您好,登錄后才能下訂單哦!
Nsstring提供了很多常用的方法,下面演示代碼:
//------------------------------------------------------------------簡單的方法
void baseMethod(){
NSString *base = @"i'm is Base Method";
NSLog(@"%@",base);
//變成大寫
base= [base uppercaseString];
NSLog(@"%@",base);
//轉(zhuǎn)換成小寫
base = [base lowercaseString];
NSLog(@"%@",base);
//首字母大寫
base = [base capitalizedString];
NSLog(@"%@",base);
}
//------------------------------------------------------------------比較方法
void compare(){
NSString *compareObject = @"Object";
//isEqualToString方法是比較內(nèi)容是否一樣
BOOL result = [compareObject isEqualToString:@"Object"];
if(result){
NSLog(@"一樣");
}else {
NSLog(@"不一樣");
}
//比較兩個(gè)字符串的的大小,返回結(jié)果是一個(gè)枚舉類型,所以千萬別用指針來接受
NSComparisonResult result2 = [compareObject compare:@"object"];
if(result2 == NSOrderedAscending){
NSLog(@"第一個(gè)比第二個(gè)字符串大");
}elseif(result2 ==NSOrderedDescending){
NSLog(@"第二個(gè)比第一個(gè)字符串大");
}elseif(result2 == NSOrderedSame){
NSLog(@"一樣");
}
//忽略大小寫進(jìn)行比較
NSComparisonResult result3= [compareObject caseInsensitiveCompare:@"object"];
if(result3 == NSOrderedAscending){
NSLog(@"第一個(gè)比第二個(gè)字符串大");
}elseif(result3 ==NSOrderedDescending){
NSLog(@"第二個(gè)比第一個(gè)字符串大");
}elseif(result3 == NSOrderedSame){
NSLog(@"一樣");
}
}
//------------------------------------------------------------------查找的方法
void nsstringSearch(){
NSString *string = @"book.txt";
//判斷是否以指定字符串結(jié)尾
BOOL result = [string hasSuffix:@".txt"];
//判斷是否以指定字符串開發(fā)
result = [string hasPrefix:@"book2"];
if(result){
NSLog(@"yes");
}else{
NSLog(@"No");
}
//根據(jù)指定的字符串進(jìn)行查找位置
NSString *string2 =@"book this is book";
NSRange rang= [string2 rangeOfString:@"book"];
NSLog(@"location:%zi",rang.location);
NSLog(@"length:%zi",rang.length);
//根據(jù)方向,對(duì)指定的字符串進(jìn)行查找位置。
rang= [string2 rangeOfString:@"book"options:NSBackwardsSearch];
NSLog(@"location:%zi",rang.location);
NSLog(@"length:%zi",rang.length);
//指定范圍進(jìn)行搜索,進(jìn)行查找
NSString *string3 = @"book is the book";
NSRange rang4 = NSMakeRange(4, 12);
rang= [string3 rangeOfString:@"book"options:NSBackwardsSearchrange:rang4];
NSLog(@"location:%zi",rang.location);
NSLog(@"length:%zi",rang.length);
}
//------------------------------------------------------------------字符串截取
void stringSplit(){
NSString *string = @"hello world";
//根據(jù)范圍進(jìn)行截取字符串
NSString *result = [string substringWithRange:[string rangeOfString:@"hello"]];
//根據(jù)指定的位置進(jìn)行截取字符串(包含指定位置的字符串)
result= [string substringFromIndex:3];
//從第一位開始截取到指定的位置
result= [string substringToIndex:2];
NSLog(@"%@",result);
NSString *number=@"1-2-3-4-5-6-7-8-9";
//根據(jù)份分割符進(jìn)行分割字符串
NSArray *arry= [number componentsSeparatedByString:@"-"];
NSLog(@"%@",arry);
}
//------------------------------------------------------------------路徑類的方法
void stringPat(){
// 將數(shù)組中的參數(shù)拼湊成地址
NSArray *array = [[NSStringstringWithUTF8String:"-Users-calvin-Desktop"]componentsSeparatedByString:@"-"];
NSString *path= [NSStringpathWithComponents:array];
NSLog(@"%@",path);
//講地址分割成數(shù)組
array = [path pathComponents];
NSLog(@"%@",array);
//判斷是否絕對(duì)路徑,其實(shí)就是判斷開頭是否未“/”。
bool bo= [path isAbsolutePath];
if(bo){
NSLog(@"是");
}else{
NSLog(@"否");
}
//獲取最后一個(gè)目錄
NSString *dir= [path lastPathComponent];
NSLog(@"%@",dir);
//刪除最后一個(gè)目錄
dir= [path stringByDeletingLastPathComponent];
NSLog(@"%@",dir);
//最后增加一個(gè)目錄,系統(tǒng)會(huì)自動(dòng)加上"/",不用自己加上
dir = [path stringByAppendingPathComponent:@"我的目錄"];
NSLog(@"%@",dir);
}
//------------------------------------------------------------------轉(zhuǎn)換的方法
void stringBaseMethod(){
NSString *string =@"do you know the Object-c";
//獲取字符串的長度,返回結(jié)果可以使用int類型進(jìn)行接收,但是OC不建議我們這樣接收。因?yàn)?span >NSUInteger就是一個(gè)無符號(hào)long類型。強(qiáng)把long類型轉(zhuǎn)換成int類型會(huì)出現(xiàn)缺失數(shù)據(jù)。
NSUInteger length = [string length];
NSLog(@"%zi",length);
//獲取指定位置的字符
unichar charAt = [string characterAtIndex:5];
NSLog(@"%c",charAt);
//把字符串轉(zhuǎn)換成int對(duì)象
NSString *number =@"23";
NSUInteger numbers = [number intValue];
NSLog(@"%zi",numbers);
//轉(zhuǎn)換成double類型
double doubles = [number doubleValue];
NSLog(@"%f",doubles);
//轉(zhuǎn)換成float類型
float floats = [number floatValue];
NSLog(@"%f",floats);
//轉(zhuǎn)換成常量的C語言字符串
constchar* charString = [number UTF8String];
NSLog(@"%s",charString);
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。