IOS NSString 截取,objectAtIndex,rangeOfString,stringWithContentsOfFile,NSEnumerator


小結:

1)componentsSeparatedByString:截取指定字符串;

2) pathForResource:獲取程序運行時目錄

3)  objectAtIndex:獲取當前索引的字符串;

4)  rangeOfString:獲取指定短字符串在長字符串中的開始,結尾索引值;

5) stringWithContentsOfFile:按行讀取文件

6) componentsSeparatedByString:@"\n"];換行分割字符串;

7) NSEnumerator *nse = [lines objectEnumerator]; 將數組轉換為NSEnumerator,可向前讀取數據;

8)  nextObject:讀取下一行數據;

 

void splitString(){

NSString *animals = @"dog#cat#pig";

//#分隔的字符串轉換成數組

NSArray *array = [animals componentsSeparatedByString:@"#"];

NSLog(@"animals:%@",array);

//獲取程序運行時目錄

NSString *escapedPath = [[NSBundle mainBundle] pathForResource:@"info" ofType:@"plist"];

NSArray *strings = [escapedPath componentsSeparatedByString: @"/"];

NSString *tmpFilename  = [strings objectAtIndex:[strings count]-1];

NSRange iStart = [escapedPath rangeOfString : tmpFilename];

NSString *runtimeDirectory = [escapedPath substringToIndex:iStart.location-1];

NSLog(@"runtimeDirectory:%@",runtimeDirectory);

//按行讀取文件

NSString *tmp;

NSArray *lines = [[NSString stringWithContentsOfFile:@"test.txt" encoding:nil error:nil

  componentsSeparatedByString:@"\n"];

NSEnumerator *nse = [lines objectEnumerator];

while(tmp = [nse nextObject]) {

NSLog(@"tmp:%@", tmp);

}

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM