標籤:
NSString *str1 = @"BeiJing"; NSString *str2 = @"beijing"; //全部轉為大寫 NSLog(@"%@",[str1 uppercaseString]); //全部轉為小寫 NSLog(@"%@",[str1 lowercaseString]); //首字母大寫 NSLog(@"%@",[str1 capitalizedString]); //比較兩個字串內容是否相同 BOOL b =[str1 isEqualToString:str2]; //兩個字串內容比較 //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));
<pre name="code" class="cpp">//字串的截取 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 *str2 = @"Users/CentralPerk/Desktop/test.txt"; NSLog(@"%@",[str2 pathExtension]); //添加拓展名,不需要帶. NSLog(@"%@",[str2 stringByAppendingPathExtension:@"mp3"]); //刪除拓展名,帶.一塊刪除 NSLog(@"%@",[str2 stringByDeletingPathExtension]); //字串轉為 int double float NSString *str3 = @"123"; NSLog(@"%i",[str3 intValue]); NSLog(@"%zi",[str3 length]); //取出指定位置的字元 unichar c = [str3 characterAtIndex:2]; NSLog(@"%c",c); //轉為C語言的字串 const char *s = [str3 UTF8String]; NSLog(@"%s",s); </pre><br><br>
//*****
/--------操作字串--NSString(靜態字串)--------------------- NSString *Beijing= @"北京歡迎您"; //字串的聲明 NSString *[email protected]"北京歡迎您a"; //[NSString stringWithFormat:@"I am ‘%@‘",Beijing]; //字串格式化 NSString *zhui = [Beijing stringByAppendingString:@"哈哈哈"]; //字串追加 bool b=[Beijing isEqualToString:log]; //字串比較 NSString *hh = @"http://www.sina.com.cn"; if([hh hasPrefix:@"http"]){ //尋找以http開頭的字串 NSLog(@"含有http"); }else{ NSLog(@"沒有http"); } NSString *ss = @"123"; int a = [ss intValue]+13; //字串轉int型 double dd = [ss doubleValue]+33.3; //字串轉double型 NSLog(@"%g",dd);//字串轉數組 NSString *zifuchuan [email protected]"one,two,three,four"; NSLog(@"string:%@",zifuchuan); NSArray *array = [zifuchuan componentsSeparatedByString:@","];// NSLog(@"array:%@",array); //輸出整個數組中所有元素 NSString *value = [array objectAtIndex:0]; //取出第0個元素 NSLog(@"value:%@",value);//數組轉字串 NSString * zifuchuan2 = [array componentsJoinedByString:@","]; NSLog(@"zifuchuan2:%@",zifuchuan2); //-substringToIndex: 從字串的開頭一直截取到指定的位置,但不包括該位置的字元NSString *string1 = @"This is a string";NSString *string2 = [string1 substringToIndex:3];NSLog(@"string2:%@",string2);//-substringFromIndex: 以指定位置開始(包括指定位置的字元),並包括之後的全部字元NSString *string1 = @"This is a string";NSString *string2 = [string1 substringFromIndex:3];NSLog(@"string2:%@",string2);//-substringWithRange: //按照所給出的位置,長度,任意地從字串中截取子串NSString *string1 = @"This is a string";NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];NSLog(@"string2:%@",string2);//--------操作動態字串--NSMutableString---------------------------------------------------- NSMutableString *mstr = [[NSMutableString alloc] init]; NSString *str1 = @"This is a example."; //建立可變字串 mstr = [NSMutableString stringWithString:str1]; //插入字元 [mstr insertString:@"very easy " atIndex:10]; //刪除一些字元 [mstr deleteCharactersInRange:NSMakeRange(10,5)]; //尋找並刪除 NSRange substr = [mstr rangeOfString:@"example"]; //字串尋找,可以判斷字串中是否有 if (substr.location != NSNotFound) { [mstr deleteCharactersInRange:substr]; } //重新設定字串 [mstr setString:@"This is string AAA"]; //替換字串 [mstr replaceCharactersInRange:NSMakeRange(15, 2) withString:@"BBB"]; //從第15個字串處替換掉後2個字串 //尋找第一個並替換 NSString *search = @"This is"; NSString *replace = @"An example of"; substr = [mstr rangeOfString:search]; if (substr.location != NSNotFound) { [mstr replaceCharactersInRange:substr withString:replace]; //把第1個遇到的substr替換為replace NSLog(@"%@",mstr); } //尋找全部匹配的,並替換 search = @"a"; replace = @"X"; substr = [mstr rangeOfString:search]; while (substr.location != NSNotFound) { [mstr replaceCharactersInRange:substr withString:replace]; substr = [mstr rangeOfString:search]; } NSLog(@"%@",mstr);常見的NSString方法 +(id)stringWithContentsOfFile:path encoding:enc error:err建立一個新字串並將其設定為path指定的檔案的內容,使用字元編碼enc,如果非零,則返回err中的錯誤。+(id)stringWithContentsOfURL:url encoding:enc error:err床架一個新字串,並將其設定為url的內容,使用字元編碼enc,如果非零,則返回err中的錯誤。+(id)string 建立一個新的Null 字元串。+(id)stringWithString:nsstring 建立一個新字串,並將其設定為nsstring-(id)initWithString:nsstring 將新分配的字串設定為nsstring-(id)initWithContentsOfFile:path encoding:enc error:err將字串設定為path指定的檔案的內容-(id) initWithContentsOfURL:url encoding;enc error:err將字串設定為url(NSURL*)url的內容,使用字元編碼enc,如果非零,則返回err中的錯誤。-(UNSIgned int)lengtn 返回字串中字元數目-(unichar) characterAtIndex:i 返回索引i的Unicode字元-(NSString*)substringFromIndex:i 返回從i開始直到結尾的字串-(NSString*)substringToIndex:i 返回從該字串開始位置到索引i的子字串 -(NSComparator *)caseInsensitiveCompare:nsstring 比較兩個字串,忽略大小寫-(NSComparator *)compare:nsstring 比較兩個字串-(BOOL)hasPrefix:nsstring 測試字串是否以nsstring開始-(BOOL)isEqualToString:nsstring 測試兩個字串是否相等。-(NSString*)capitalizedString 返回每個單字首大寫的字串(每個單詞的其餘字母轉換為小寫) -(NSString *)lowercaseString 返迴轉換為小寫字串-(NSString *)uppercaseString 返回抓換為大寫的字串-(const char *)UTF8String 返迴轉換為UTF8字串的字串-(double) doubleValue 返迴轉換為NSInteger整數的字串-(int)intValue 返迴轉換為整數的字串NSMutableString字元方法+(id)stringWithCapacity:size 建立一個字串,初始包含size的字元-(id)initWithCapacity:size 使用初始容量為size的字元來初始化字串-(void)setString :nsstring 將字串設定為nsstring-(void) appendString:nsstring 在接受者的末尾附加nsstring-(void)deleteCharactersInrange:range 刪除指定range中的字元-(void)insertString:nsstring atIndex:i以索引i為起始位置插入nsstring-(void) replaceCharactersInrange: range withString:nsstring 使用nsstring替換range指定的字元-(void)replaceOccurrencesOf String:nsstring withString:nsstring2 options:opts range:range根據選項opts,使用指定range中的nsstring2替換所有的nsstring。選項可以包括NSBackwardsSearch(從範圍的結尾開始搜尋),NSAn沖熱點Search(nsstring必須匹配範圍的開始),NSLiteralSearch
iOS 字串應用