-----從文件讀取字符串-----
NSString *path = @"astring.text";
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];api
-----寫字符串到文件----
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";
[astring writeToFile: path atomically: YES];
[astring release];
-----比較兩個字符串-----
//一、用C比較:strcmp函數
char string1[] = "string!";
char string2[] = "string!";
if(strcmp(string1, string2) = = 0)
{
NSLog(@"1");
}
//二、isEqualToString方法
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);
//三、compare方法(comparer返回的三種值:NSOrderedSame,NSOrderedAscending,NSOrderedDescending)
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;
NSLog(@"result:%d",result);
//NSOrderedAscending 判斷兩對象值的大小(按字母順序進行比較,astring02大於astring01爲真)緩存
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;
NSLog(@"result:%d",result);
//NSOrderedDescending 判斷兩對象值的大小(按字母順序進行比較,astring02小於astring01爲真)
//四、不考慮大小寫比較字符串1
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedDescending判斷兩對象值的大小(按字母順序進行比較,astring02小於astring01爲真)
//五、不考慮大小寫比較字符串2
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02
options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;
NSLog(@"result:%d",result);
//NSCaseInsensitiveSearch:不區分大小寫比較 NSLiteralSearch:進行徹底比較,區分大小寫 NSNumericSearch:比較字符串的字符個數,而不是字符值。app
-----改變字符串的大小寫-----
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//uppercaseString返回轉換爲大寫的字符串
NSLog(@"string2:%@",[string2 lowercaseString]);//lowercaseString返回轉換爲小寫的字符串
NSLog(@"string2:%@",[string2 capitalizedString]);//capitalizedString返回每一個單詞首字母大寫的字符串函數
-----在串中搜索子串 ----- this
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);
[astring release];atom
-----抽取子串 ----- url
//一、-substringToIndex: 從字符串的開頭一直截取到指定的位置,但不包括該位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);spa
//二、-substringFromIndex: 以指定位置開始(包括指定位置的字符),幷包括以後的所有字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);code
//三、-substringWithRange: //按照所給出的位置,長度,任意地從字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@"string2:%@",string2);
//五、枚舉
NSEnumerator *filenum;
filenum = [files objectEnumerator];
while (filename = [filenum nextObject]) {
NSLog(@"filename:%@",filename);
}
-----切分數組-----
//一、從字符串分割到數組- componentsSeparatedByString:
NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];
NSLog(@"string:%@",string);
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
[string release];
//二、從數組合並元素到字符串- componentsJoinedByString:
NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString *string = [array componentsJoinedByString:@","];
NSLog(@"string:%@",string);
-----從目錄搜索擴展名爲jpg的文件-----
//NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *home;
home = @"../Users/";
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: home];
NSMutableArray *files = [[NSMutableArray alloc] init];
//枚舉
NSString *filename;
while (filename = [direnum nextObject]) {
if([[filename pathExtension] hasSuffix:@"jpg"]){
[files addObject:filename];
}
}
//擴展路徑
NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
//文件擴展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);
-----查找與替換-----
- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement-----在已有字符串後面添加字符-----
//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);
----- 在已有字符串中按照所給出範圍刪除字符----
//deleteCharactersInRange:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)]; // 刪除指定範圍(location=0,length=5)的字符串
NSLog(@"String1:%@",String1);
----在已有字符串後面在所指定的位置中插入給出的字符串-----
//-insertString: atIndex:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);
[String1 insertString:@"and StringEnd", atIndex:[String1 length]]; // 在可變字符串的最後插入
----將已有的空符串換成其它的字符串-----
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);
----查找-----
NSRange subRange = [String1 rangeOfString:@"is a"]; // 若是沒查找到,則 (subRange.location == NSNotFound)爲真。
----按照所給出的範圍替換的原有的字符-----
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; // 用於NSMutableString
NSLog(@"String1:%@",String1);
----判斷字符串內是否還包含別的字符串(前綴,後綴)-----
//01: 檢查字符串是否以另外一個字符串開頭- (BOOL) hasPrefix: (NSString *) aString;
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
//02: 查找字符串某處是否包含給定的字符串 - (NSRange) rangeOfString: (NSString *) aString,這一點前面在串中搜索子串用到過
NSRange subRange;
subRange = [string1 rangeOfString:@"string A"]; //查找字符串string1中是否包含「string A」。返回NSRange類型。
if(subRange.location == NSNotFound)
NSLog(@"String not found ");
else NSLog(@"string is at index %lu, length is %lu", subRange.location, subRange.length);
NSDate
NSCalendarDate
NSCalendarDate對象包含了日期和時間、時區以及一個帶有格式的字符串,它從NSDate繼承而來。
NSCalendarDate對象是immutable的,一旦被建立,沒法修改其中的時間和日期,固然能夠修改那個帶格式的字符串和時區。
如下是經常使用方法:
+(id)calendarDate; //建立當前日期和時間以及默認格式的NSCalendarDate對象,時區爲機器設置好的時區。
+(id)dateWithYear:(int)year
month:(unsigned)month
day:(unsigned)day
hour:(unsigned)hour
minute:(unsigned)minute
second:(unsigned)second
timeZone:(NSTimeZone *)aTimeZone
-(int)dayOfCommonEra; //獲得從公元1年算起,有多少天
-(int)dayOfMonth; //返回是月的第幾天(1-31)
-(int)dayOfWeek; //返回是周的第幾天 (0-6)
-(int)dayOfYear; //返回是年的第幾天(1-366)
-(int)hourOfDay; // 返回是日的第幾個小時(0-23)
-(void)setCalendarFormate:(NSString *)format
--------建立NSCalendarDate對象--------
NSCalendarDate *now;
now = [NSCalendarDate calendarDate];
NSTimeZone *pacific = [NSTimeZone timeZoneWithName:@"PST"];
NSCalendarDate *hotTime = [NSCalendarDate dateWithYear:2011 month:2 day:3 hour:14 minute:0 second:0 timeZone:pacific];
NSData
使用文件時,須要頻繁地將數據讀入一個臨時存儲區,它一般成爲緩衝區。
NSData類提供了一種簡單的方式,它用來設置緩衝區、將文件的內容讀入緩衝區,或將緩衝區的內容寫到一個文件。
對於32位應用程序,NSDATA緩存區最多能夠存儲2GB的數據。
咱們既可定義不變緩衝區(NSData類),也可定義可變的緩衝區(NSMutableData類)。
下面代碼展現瞭如何將文件的內容讀入內存緩衝區,而後再將緩衝區的內容寫入到另外一個文件中。
NSData *fileData; NSFileManager *fileManager = [[NSFileManager alloc]init]; fileData = [fileManager contentsAtPath:path]; [fileManager createFileAtPath:path2 contents:fileData attributes:nil]; //採用默認的屬性值
類型轉換 NSData -> NSString:
NSString *strData = [[NSString alloc]initWithData:fileData encoding:NSASCIIStringEncoding];
類型轉換 NSString -> NSData:
NSData *fileData2 = [strData dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData