NSString *filePath = [[NSBundle mainBundle] pathForResource:@"str" ofType:@"txt"];
NSError *error;
NSString *fileStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSError *error;
NSString *urlStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSString *str = [NSString string];
NSString *str1 = @"字符串1";
NSString *str2 = [NSString stringWithString:str1];
NSString *str = @"字符串1";
NSLog(@"%lu",str.length);
NSString *str = @"abcd";
NSLog(@"%hu",[str characterAtIndex:2]);
//从指定位置开始截取字符串
NSString *str = @"abcd";
NSLog(@"%@",[str substringFromIndex:1]);
//从指定位置截取指定数目的字符串
NSString *str = @"abcd";
NSLog(@"%@",[str substringWithRange:NSMakeRange(1, 2)]);
//从开始位置截取到指定位置(不包含边界)
NSString *str = @"abcd";
NSLog(@"%@",[str substringToIndex:2]);
NSString *str = @"abcd";
NSLog(@"%d",[str hasPrefix:@"abc"]);
NSString *str = @"abcd";
NSLog(@"%d",[str hasSuffix:@"bcd"]);
NSString *str = @"abcd";
NSLog(@"%d",[str isEqualToString:@"abco"]);
NSString *str = @"ABCD";
NSLog(@"%@",[str lowercaseString]);
NSString *str = @"abcd";
NSLog(@"%@",[str uppercaseString]);
NSString *str = @"0";
double dStr = [str doubleValue];
float fStr = [str floatValue];
int iStr = [str intValue];
NSInteger nStr = [str integerValue];
BOOL bStr = [str boolValue];
NSString *str = @"abcd";
NSRange range = [str rangeOfString:@"ab"];
NSLog(@"%@",NSStringFromRange(range));
//NSCaseInsensitiveSearch 不区分字母大小写
//NSLiteralSearch 对字符串进行字节单位的比较,一般可提高检索速度
//NSBackwardsSearch 从范围的末尾开始检索
//NSAnchoredSearch 仅检索制定范围的前部。忽略字符串中间的检索字符
NSString *str = @"AbcD";
NSRange range = [str rangeOfString:@"ab" options:NSCaseInsensitiveSearch];
if(range.location != NSNotFound){
NSLog(@"%@",NSStringFromRange(range));
}
NSString *str = @"AbcD";
NSString *newString = [str stringByReplacingOccurrencesOfString:@"A" withString:@"y"];
NSString *string = @"A|B|C|D";
NSArray *array = [string componentsSeparatedByString:@"|"];
NSMutableString *mStr = [NSMutableString stringWithCapacity:4];
NSMutableString *mStr = [NSMutableString stringWithCapacity:4];
[mStr setString:@"abcdef"];
NSLog(@"%@",mStr);
NSMutableString *mStr = [NSMutableString stringWithCapacity:4];
[mStr appendString:@"abcdef"];
NSLog(@"%@",mStr);
NSMutableString *mStr = [NSMutableString stringWithCapacity:4];
[mStr appendString:@"abcdef"];
[mStr deleteCharactersInRange:NSMakeRange(1,2)];
NSLog(@"%@",mStr);
NSMutableString *mStr = [NSMutableString stringWithCapacity:4];
[mStr appendString:@"abcdef"];
[mStr insertString:@"hij" atIndex:2];
NSLog(@"%@",mStr);
%@ NSString实例
%d,%D,%i 整数
%4d,%4D,%4i 格式化整数
%ld,%lD,%li 长整数
%u,%U 无符号整数
%x 将无符号整数以十六进制小写字母显示
%X 将无符号整数以十六进制大写字母显示
%f 小数
%c 字符
%s C语言字符串
%% 显示%字符本身