for (id key in paramValues_){ NSLog(@"key: %@ ,value: %@",key,[paramValues_ objectForKey:key]); }
NSString *path = @"/Users/Allen/Desktop/test.dat";
NSString *temp = @"Allen Dang";
int i = 100;
float f = 98.3333f;
NSMutableData *writer = [[NSMutableData alloc] init];
[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
[writer appendBytes:&i length:sizeof(i)];
[writer appendBytes:&f length:sizeof(f)];
[writer writeToFile:path atomically:YES];
[writer release];
读文件
NSString *contentPath = [[NSBundle mainBundle] pathForResource:@"Content" ofType:@"txt"];
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *txtContent = [NSString stringWithContentsOfFile:contentPath encoding:NSUTF8StringEncoding error:nil];
写文件
NSString *str = @"asdasdads";
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData *data = [str dataUsingEncoding:enc];
NSString *filename = @"/Users/xxx/files.txt"];
[data writeToFile: filename atomically: NO];
@interface NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString;
- (NSString *)URLDecodedString;
@end
@implementation NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString{
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8);
[result autorelease];
return result;
}
- (NSString*)URLDecodedString{
NSString *result = (NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8);
[result autorelease];
return result;
}
@end
NSString* aStr = [[NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];
NSData* aData = [aStr dataUsingEncoding: NSASCIIStringEncoding];
NSString方法
+(id) stringWithContentsOfFile:path encoding:enc error:err
创建一个新字符串并将其设置为path指定的文件的内容,使用字符编码enc,如果非零,则返回err中错误
+(id) stringWithContentsOfURL:url encoding:enc error:err
创建一个新的字符串,并将其设置为url的内容,使用字符编码enc,如果非零,则返回err中的错误
+(id) string
创建一个新的空字符串
+(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中的错误
-(id) (UNSIgned int)length
返回字符串中的字符数目
-(unichar)characterAtIndex:i
返回索引i的Unicode字符
-(NSString *)substringFromIndex:i
返回从i开始知道结尾的子字符串
-(NSString *)substringWithRange:range
根据指定范围返回子字符串
-(NSString *)substringToIndex:i
返回从该字符串开始到索i的子字符串
-(NSComparator *)caseInsensitiveCompare:nsstring
比较两个字符串,忽略大小写
-(NSComparator *)compare:nsstring
比较两个字符串
-(BOOL)hasPrefix:nsstring
测试字符串是否以nsstring开始
-(BOOL)hasSuffix:nsstring
测试字符串是否以nsstrng结尾
-(BOOL)isEqualToString:nsstring
测试两个字符串是否相等
-(NSString *) capitalizedString
返回每个单词首字母大写的字符串(每个单词的其余字母转换为小写)
-(NSString *)lowercaseString
返回转换为小写的字符串
-(NSString *)uppercaseString
返回转换为大写的字符串
-(const char*)UTF8String
返回转换为UIF-8字符串的字符串
-(double)doubleValue
返回转换为double的字符串
-(float)floatValue
返回转换为浮点值的字符串
-(NSInteger)integerValue
返回转换为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(从范围的结尾 开始搜索)NSAnchoredSearch(nsstring必须匹配范围的开始),NSLiteralSearch(执行逐字节比较以及 NSCaceInsensitiveSearch的按位或组合)
*----------------在串中搜索子串----------------*/
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];
/*----------------抽取子串 ----------------*/
//-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);