Objective-C 【NSRange&字串的截取和替換】,objective-cnsrange
———————————————————————————————————————————
NSString NSRange的使用
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 首先明確NSRange是一個結構體類型(是表示範圍的結構體類型)
// typedef struct _NSRange {
// NSUInteger location;//表示位置
// NSUInteger length;//表示長度
// } NSRange;
// NSRange 對象的幾種建立方式
// ①直接對NSRange類型的執行個體對象賦值
NSRange range1={3,4};
NSLog(@"range1.location = %ld,range1.length = %ld",range1.location,range1.length);
// ②先建立對象,後賦值
NSRange range2;
range2.location=5;
range2.length=6;
NSLog(@"range1.location = %ld,range1.length = %ld",range2.location,range2.length);
// ③調用NSMakeRange方法賦值
NSRange range3=NSMakeRange(11, 12);
NSLog(@"range1.location = %ld,range1.length = %ld",range3.location,range3.length);
}
return 0;
}
———————————————————————————————————————————
NSString 字串的截取和替換
#import <Foundation/Foundation.h>
void test1()
{
// 字串的截取
// ①從字串的指定位置截取到字串末尾(包含指定位置的字元,下標從0開始)
NSString *str1=@"1234567890";
NSString *newStr1=[str1 substringFromIndex:6];
NSLog(@"newStr1 = %@",newStr1);
// ②從字串開頭截取到字串的指定位置(不包含指定位置的字元,下標從0開始)
NSString *str2=@"abcdefghijklmnopq";
NSString *newStr2=[str2 substringToIndex:3];
NSLog(@"newStr2 = %@",newStr2);
// ③從字串的指定位置1 截取 指定長度
NSString *str3=@"12312fsg34fav";
NSRange range={3,6};//涉及到截取位置和長度,自然要用到NSRange了。所以定義一個NSRange類型的對象來設定位置和長度參數
NSString *newStr3=[str3 substringWithRange:range];//再用substringWithRange函數調用我們建立好的NSRange類型的對象以完成截取
NSLog(@"newStr3 = %@",newStr3);
}
void test2()
{
// 小練習: 將下面標籤內的字串取出
// <itcast>王中堯</itcast>
NSString *str=@"<itcast>王中堯</itcast>";
NSRange range=NSMakeRange(8, 3);
NSString *newStr=[str substringWithRange:range];
NSLog(@"newStr = %@",newStr);
}
void test3()
{
// 一個小小的拔高題目:(喜歡的讀者可以試著做一下這個題目)
// 統計一段話中,指定片語的個數。
// 有一天,我問醫生:“我為什麼咳嗽?” 醫生說:“你10歲的時候咳嗽嗎?” 我說:“不咳嗽。” 醫生又問:“你20歲的時候咳嗽嗎?” 我說:“不咳嗽。” 醫生又問:“你30歲的時候咳嗽嗎?” 我說:“不咳嗽。” 醫生說:“那你現在確實該咳嗽一下了!”
// 統計上面這段話中 咳嗽 出現的次數
NSString *str=@"有一天,我問醫生:“我為什麼咳嗽?” 醫生說:“你10歲的時候咳嗽嗎?” 我說:“不咳嗽。” 醫生又問:“你20歲的時候咳嗽嗎?” 我說:“不咳嗽。” 醫生又問:“你30歲的時候咳嗽嗎?” 我說:“不咳嗽。” 醫生說:“那你現在確實該咳嗽一下了!”";
int n=0;
while ([str rangeOfString:@"咳嗽"].location!=NSNotFound) {
//先在str中搜尋指定字串,如果字串的位置可找到,那麼說明str中存在一個指定字串,所以要讓n+1
n++;
str=[str substringFromIndex:[str rangeOfString:@"咳嗽"].location+2];
//再在這裡截取前一個制定字串後(不包括前一個指定字串,所以位置要+2)剩下字串
//位置+2,2正式“咳嗽”的字串長度
}
NSLog(@"n = %d",n);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 字串的替換
// 這一部分知識很簡單,只需要調用一個 對象方法 即可
// stringByReplacingOccurrencesOfString:str1 withString:str2 這個方法是將某字串中的 所有 str1 替換為 str2
// ①將 <itcast>王中堯</itcast> 中的itcast替換為ios
NSString *str=@"<itcast>王中堯</itcast>";
str=[str stringByReplacingOccurrencesOfString:@"itcast" withString:@"ios"];
NSLog(@"str = %@",str);
// ②將 http:** ios.itcast.cn *ios*images* content_25.jpg 中的 空格去掉,然後將 * 替換為 /
NSString *str2=@"http:** ios.itcast.cn *ios*images* content_25.jpg";
// 顯然替換兩次即可
str2=[str2 stringByReplacingOccurrencesOfString:@" " withString:@""];//空 表示為 兩個雙引號之間什麼都不寫
str2=[str2 stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
NSLog(@"str2 = %@",str2);
}
return 0;
}
———————————————————————————————————————————
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。