2017年4月29日 星期六
2016年12月1日 星期四
NSMutableString replace 字串置換
NSMutableString replace 字串置換方法
!!!Range定義 typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; location其實就是index length表示從index開始幾個字元
...NSMutableString *myMSstr1; NSMutableString *myMSstr2; myMSstr1 = [NSMutableString stringWithFormat: @"Please don't see."]; // replace a range of chars with another (replace:置換)(將Range範圍內的字元替換) [myMSstr1 replaceCharactersInRange: NSMakeRange(7, 10) withString: @"see me."]; NSLog(@"%@", myMSstr1); myMSstr2 = [NSMutableString stringWithFormat: @"%@%@%@%@%@", myMSstr1, myMSstr1, myMSstr1, myMSstr1, myMSstr1]; NSString *search, *replace; NSRange RangeOfStr; // Search and replace search = @"see me."; replace = @"don't see."; RangeOfStr = [myMSstr1 rangeOfString: search]; if(RangeOfStr.location != NSNotFound){ [myMSstr1 replaceCharactersInRange: RangeOfStr withString: replace]; NSLog(@"%@", myMSstr1); } // Search and replace All 搜尋所有search字串置換為replace字串 RangeOfStr = [myMSstr2 rangeOfString: search]; while(RangeOfStr.location != NSNotFound){ [myMSstr2 replaceCharactersInRange: RangeOfStr withString: replace]; NSLog(@"%@", myMSstr2); RangeOfStr = [myMSstr2 rangeOfString: search]; } 執行結果:
2016年11月30日 星期三
NSString::取得字串長度(Get NSString Length)
2016年7月11日 星期一
Objective Static
Objective Static
…靜態變數預設的初始值為0,且執行期間只會被初始一次,以下使用Static關鍵字實現方法呼叫次數
@interface Page: NSObject -(void) showTimes; @end @implementation Page -(void) showTimes { static int PageTimes; ++PageTimes; NSLog(@"showTimes Call Times : %i", PageTimes); } @end int main(int argc, const char * argv[]) { @autoreleasepool { Page *myPage = [Page new]; [myPage showTimes]; Page *myPage2 = [Page new]; [myPage2 showTimes]; Page *myPage3 = [Page new]; [myPage3 showTimes]; } return 0; }![]()
訂閱:
文章 (Atom)