iOS NSString的常用用法(史上最全)
NSString *str2 = @"beijing";
//全部轉為大寫
NSLog(@"%@",[str1 uppercaseString]);
//全部轉為小寫
NSLog(@"%@",[str1 lowercaseString]);
//首字母大寫
NSLog(@"%@",[str1 capitalizedString]);
//比較兩個字串內容是否相同
BOOL b =[str1 isEqualToString:str2];
//5.兩個字串內容比較
//NSOrderedAscending 右邊 > 左邊
//NSOrderedSame 內容相同
//NSOrderedDescending 左邊 > 右邊
NSComparisonResult result = [str1 compare:str2];
if (result == NSOrderedAscending) {
NSLog(@"右邊 > 左邊");
}else if(result == NSOrderedSame){
NSLog(@"內容相同");
}else if (result == NSOrderedDescending){
NSLog(@"左邊 > 右邊");
}
//忽略大小寫進行比較,返回值與compare一樣
result = [str1 caseInsensitiveCompare:str2];
if (result == NSOrderedAscending) {
NSLog(@"右邊 > 左邊");
}else if(result == NSOrderedSame){
NSLog(@"內容相同");
}else if (result == NSOrderedDescending){
NSLog(@"左邊 > 右邊");
}
//判斷字串是否以指定字串開頭
[str1 hasPrefix:@"aaa"];
//判斷字串是否以指定字串結尾
[str1 hasSuffix:@"aaa"];
//判斷字串是否包含指定字串,返回位置和長度
NSRange range = [@"123456" rangeOfString:@"456"];
NSLog(@"%@", NSStringFromRange(range));
//反向搜尋
range = [@"123456456qweasasd456" rangeOfString:@"456" options:NSBackwardsSearch];
NSLog(@"%@",NSStringFromRange(range));
//指定範圍進行搜尋
range = NSMakeRange(0, 9);
range = [@"123456456qweasasd456" rangeOfString:@"456" options:NSBackwardsSearch range:range];
NSLog(@"%@",NSStringFromRange(range));
//字串的擷取
NSString *str = @"123456789";
NSLog(@"%@",[str substringFromIndex:3]);
NSLog(@"%@",[str substringToIndex:6]);
NSLog(@"%@",[str substringWithRange:NSMakeRange(3, 3)]);
//用指定字串分割字串,返回一個數組
NSArray *array = [@"1,2,3,4,5,6" componentsSeparatedByString:@","];
NSLog(@"%@",array);
//將陣列中的字串組合成一個檔案路徑
NSMutableArray *components = [NSMutableArray array];
[components addObject:@"Users"];
[components addObject:@"CentralPerk"];
[components addObject:@"Desktop"];
NSString *path = [NSString pathWithComponents:components];
NSLog(@"%@",path); //Users/CentralPerk/Desktop
//將一個路徑分割成一個數組
NSArray *array1 = [path pathComponents];
NSLog(@"%@",array1);
//判斷是否為絕對路徑(依據:是否以'/'開始)
path = @"/Users/CentralPerk/Desktop";
NSLog(@"%i",[path isAbsolutePath]);
//獲取最後一個目錄
NSLog(@"%@",[path lastPathComponent]);
//刪除最後一個目錄
NSLog(@"%@",[path stringByDeletingLastPathComponent]);
//拼接一個目錄
NSLog(@"%@",[path stringByAppendingPathComponent:@"aaa"]); ///Users/CentralPerk/Desktop/aaa
NSLog(@"%@",[path stringByAppendingString:@"aaa"]); ///Users/CentralPerk/Desktopaaa
NSLog(@"%@",[path stringByAppendingFormat:@"%@%@",@"b",@"c"]); ///Users/CentralPerk/Desktopbc
//拓展名出來
//獲取拓展名,不帶.
NSString *str3 = @"Users/CentralPerk/Desktop/test.txt";
NSLog(@"%@",[str3 pathExtension]);
//新增拓展名,不需要帶.
NSLog(@"%@",[str3 stringByAppendingPathExtension:@"mp3"]);
//刪除拓展名,帶.一塊刪除
NSLog(@"%@",[str3 stringByDeletingPathExtension]);
//字串轉為 int double float
NSString *str4 = @"123";
NSLog(@"%i",[str4 intValue]);
NSLog(@"%zi",[str4 length]);
//取出指定位置的字元
unichar c = [str4 characterAtIndex:2];
NSLog(@"%c",c);
//轉為C語言的字串
const char *s = [str4 UTF8String];
NSLog(@"%s",s);
//去掉兩端的空格
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//去掉多餘的空格
NSString *str = @" this is a test . ";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
str = [filteredArray componentsJoinedByString:@" "];
//去掉所有空格
[str stringByReplacingOccurrencesOfString:@" " withString:@""]
//1、建立常量字串。
NSString *astring = @"This is a String!";
//2、建立空字串,給予賦值。
NSString *astring = [[NSString alloc] init];
astring = @"This is a String!";
NSLog(@"astring:%@",astring);
//
NSString *astring = [[NSString alloc] init];
NSLog(@"0x%.8x", astring);
NSLog(@"0x%.8x", astring);
NSLog(@"astring:%@",astring);
//3、在以上方法中,提升速度:initWithString方法
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
//4、用標準c建立字串:initWithCString方法
char *Cstring = "This is a String!";
NSString *astring = [[NSString alloc] initWithCString:Cstring];
NSLog(@"astring:%@",astring);
//5、建立格式化字串:佔位符(由一個%加一個字元組成)
int i = 1;
int j = 2;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
NSLog(@"astring:%@",astring);
//6、建立臨時字串
NSString *astring;
astring = [NSString stringWithCString:"This is a temporary string"];
NSLog(@"astring:%@",astring);
//7、從檔案建立字串
NSString *path = [[NSBundlemainBundle] pathForResource:@"astring.text"ofType:nil];
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
//8、用字串建立字串,並寫入到檔案
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";//注:此路徑path只只是示意,真實路徑並非如此
[astring writeToFile: path atomically: YES];
//9、用C比較:strcmp函式
char string1[] = "string!";
char string2[] = "string!";
if(strcmp(string1, string2) == 0){
NSLog(@"1");
}
//10、isEqualToString方法
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);
//11、compare方法(comparer返回的三種值)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] == NSOrderedSame; //NSOrderedSame判斷兩者內容是否相同
NSLog(@"result:%d",result);
//
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] == NSOrderedAscending; //NSOrderedAscending判斷兩物件值的大小(按字母順序進行比較,astring02大於astring01為真)
NSLog(@"result:%d",result);
//
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] == NSOrderedDescending; //NSOrderedDescending判斷兩物件值的大小(按字母順序進行比較,astring02小於astring01為真)
NSLog(@"result:%d",result);
//12、不考慮大小寫比較字串
//1.
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] == NSOrderedSame; //NSOrderedDescending判斷兩物件值的大小(按字母順序進行比較,astring02小於astring01為真)
NSLog(@"result:%d",result);
//2.
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02
options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame; //NSCaseInsensitiveSearch:不區分大小寫比較 NSLiteralSearch:進行完全比較,區分大小寫 NSNumericSearch:比較字串的字元個數,而不是字元值。
NSLog(@"result:%d",result);
//13、輸出大寫或者小寫字串
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//大寫
NSLog(@"string2:%@",[string2 lowercaseString]);//小寫
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小
//14、-rangeOfString: //查詢字串某處是否包含其它字串
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
//15、-substringToIndex: 從字串的開頭一直擷取到指定的位置,但不包括該位置的字元
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
//16、-substringFromIndex: 以指定位置開始(包括指定位置的字元),幷包括之後的全部字元
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
//17、-substringWithRange: //按照所給出的位置,長度,任意地從字串中擷取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@"string2:%@",string2);
//18、-stringWithCapacity: //按照固定長度生成空字串
NSMutableString *String;
String = [NSMutableString stringWithCapacity:40];
//19、-appendString: and -appendFormat: //把一個字串接在另一個字串的末尾
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 appendString:@", I will be adding some character"];
[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];
NSLog(@"String1:%@",String1);
//20、-insertString: atIndex: //在指定位置插入字串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);
//21、-setString: 替換字串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);
//22、-replaceCharactersInRange: withString: //用指定字串替換字串中某指定位置、長度的字串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];
NSLog(@"String1:%@",String1);
//23、-hasPrefix: //檢查字串是否以另一個字串開頭
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
//24、擴充套件路徑
NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
//25、副檔名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);
//26、剪下 字串 ,按照range的要求,從第幾個開始 , 剪下的長度為幾
NSMutableString *a = [[NSMutableString alloc] initWithString:@"123456798"];
NSLog(@" \n a: %@ \n",a);
[a deleteCharactersInRange:NSMakeRange(1, 2)];
NSLog(@" \n a: %@ \n",a);
// 2011-07-05 20:59:34.169 Q[9069:207] a: 123456798
// 2011-07-05 20:59:34.171 Q[9069:207] a: 1456798
相關推薦
iOS NSString的常用用法(史上最全)
NSString *str1 = @"BeiJing"; NSString *str2 = @"beijing"; //全部轉為大寫 NSLog(@"%@",[str1 uppercaseString]); //全部轉為小寫 NSLog(@"%@
iOS學習之Swift史上最全第三方輪子大全
@SwiftLanguage 更新至 2016-2-1,最近新收錄 Graph, Localize-Swift, Cuckoo, Gecco, AudioKit, vapor, Every.swift 等 7 個,合計已收錄 297 個。詳見本文件。 工具類 專案 開
iOS常用第三方庫大全,史上最全第三方庫收集
下拉重新整理 MJRefresh – 僅需一行程式碼就可以為UITableView或者CollectionView加上下拉重新整理或者上拉重新整理功能。可以自定義上下拉重新整理的文字說
React Native常用第三方組件匯總--史上最全 之一
提示 存儲 ext upload body ner board pup wan 把我認為最好的知識,拿來與他人分享,是這一生快事之一! React Native 項目常用第三方組件匯總: react-native-animatable 動畫 react-na
史上最全常用正則表示式大全
一、校驗數字的表示式 1. 數字:^[0-9]*$ 2. n位的數字:^\d{n}$ 3. 至少n位的數字:^\d{n,}$ 4. m-n位的數字:^\d{m,n}$ 5. 零和非零開頭的數字:^(0|[1-9][0-9]*)$ 6. 非零開頭的最多帶兩位小
史上最全的HTML和CSS標籤常用命名規則
資料夾主要建立以下資料夾: 1、Images 存放一些網站常用的圖片; 2、Css 存放一些CSS檔案; 3、Flash 存放一些Flash檔案; 4、PSD 存放一些PSD原始檔; 5、Temp 存放所有臨時圖片和其它檔案; 6、copyright
新手必看,史上最全的iOS開發教程集錦,沒有之一!
最近大火的iPhone XS Max和iPhone XS,不知道有沒有同學已經下手了呢?一萬三的價位確實讓很多人望而卻步啊。據說為了贏得中國的使用者,專門出了雙卡雙待的,可想而知中國市場這塊“肥肉”人人都想要。 近幾年,無論蘋果出什麼樣的產品以及多高的價位,都會有非常多的蘋
史上最全的iOS面試題及答案
最近在做iOS面試,總結一些實用的面試題以及參考答案,供博友們交流溝通。 可用一些不明確的技術要點引起話題,如: Multithreading:什麼時候處理多執行緒,幾種方式,優缺點。 Delegate, Notification,KVO, other 優
嚴選 | Elasticsearch史上最全最常用工具清單【轉】
1、題記 工欲善其事必先利其器,ELK Stack的學習和實戰更是如此,特將工作中用到的“高效”工具分享給大家。 希望能借助“工具”提高開發、運維效率! 2、工具分類概覽 2.1 基礎類工具 1、Head外掛 1)功能概述: ES叢集狀態檢視、索引資料檢視、ES DSL實現(增、刪、改、查操作)比較實用的地方
史上最全的程式設計師常用英語詞彙 珍藏版
A abstract 抽象的 abstract base class (ABC)抽象基類 abstract class 抽象類 abstraction 抽象、抽象物、抽象性 access 存取、訪問 access function 訪問函式 access lev
iOS開發應用上架必讀最新蘋果稽核規則(史上最全版)
http://blog.csdn.net/zc639143029/article/details/51234645 學習交流及技術討論可新浪微博關注:極客James 1. 條款和條件 • 1.1 為App Store開發程式,開發者必須遵守 Program Li
史上最全Python基礎合集!集合用法、檔案操作字元轉換、函式
list = {1, 3, 6, 5, 7, 9, 11, 3, 7} # 定義集合方式一 list1 = set([1, 3, 6, 5, 7, 9, 11, 3, 7]) # 定義集合方式二 list2 = set() # 定義一個空集合 p
史上最全常用正則表示式大全 史上最全常用正則表示式大全
史上最全常用正則表示式大全 很多不太懂正則的朋友,在遇到需要用正則校驗資料時,往往是在網上去找很久,結果找來的還是不很符合要求。所以我最近把開發中常用的一些正則表示式整理了一下,在這裡分享一下。給自己留個底,也給朋友們做個參考。 一、校驗數字的表
React Native常用第三方元件彙總--史上最全
快取管理https://github.com/reactnativecn/react-native-http-cacheListView的優化https://github.com/sghiassy/react-native-sglistview圖片和base64互轉https://github.com/xfu
【備忘】史上最全 18套 IOS 專案實戰 下載
09-【遊戲開發】千峰-憤怒的小鳥-2015-07-08 23:4904-【專案實戰】飛盒-仿淘寶客戶端專案-2015-07-08 23:4903-【專案實戰】飛盒-筆記本專案-2015-07-08 23:4902-【專案實戰】ios實戰-2015-07-08 23:490
iOS開發之第三方登入微信-- 史上最全最新第三方登入微信方式實現
// // ViewController.m // weixinLoginDemo // // Created by 張國榮 on 16/6/20. // Copyright © 2016年 BateOrganization. All rights reserved. // #import "Vie
史上最全的常用開發工具類收集(持續更新中)
API checkBankCard : 校驗銀行卡卡號是否合法 getBankCardCheckCode: 從不含校驗位的銀行卡卡號採用 Luhm 校驗演算法獲得校驗位 getNameOfBank : 通過銀
【轉載】史上最全:TensorFlow 好玩的技術、應用和你不知道的黑科技
tube map 高性能 知識 seq 出現 執行時間 mes lex 【導讀】TensorFlow 在 2015 年年底一出現就受到了極大的關註,經過一年多的發展,已經成為了在機器學習、深度學習項目中最受歡迎的框架之一。自發布以來,TensorFlow 不斷在完善並增加新
史上最全: svn與git的對照(二):svn與git的相關概念
fill 來看 out avi head clas 相關 iss b2c 如圖1是svnserver端數據的文件夾結構 以下是gitserver端的文件夾結構 縱觀svn和git服務端的文件夾結構我們非常easy發現 1.有些目錄還是蠻像的。甚
掛載文件系統出現"kernel panic..." 史上最全解決方案
某個文件 table sha mount nic mic 2.6 完成 又是 問:掛載自己制作的文件系統卡在這裏: NET: Registered protocol family 1 NET: Registered protocol family 17