在iOS系统,NSString可能是最常用的对象,很多用法跟其他语言不一样。
字符串对象NSString 使用格式创建字符串 1
2
3
4
5
+ (id )stringWithFormat:(NSString *)format...
- (id )initWithFormat:(NSString *)format...
NSString *str = "hello" ;
NSString *string = [NSString stringWithFormat:@"%@ world" ,str];
常用的替换符 1
2
3
4
5
6
7
8
9
10
11
12
13
14
%@ OC对象描述
%zd NSInteger
%tu NSUInteger
%d,%D,%i 整数
%4d,%4D,%4i 格式化整数
%ld,%lD,%li 长整数
%u,%U 无符号整数
%x 将无符号整数以十六进制小写字母显示
%X 将无符号整数以十六进制大写字母显示
%f 小数
%c 8位无符号字符
%C 16位UNICODE字符
%s C语言字符串
%% 显示%字符本身
范围集合NSRange 定义 1
2
3
4
typedef struct _NSRange {
unsigned int location;
unsigned int length;
} NSRange ;
NSMakeRange函数 这个函数比较特殊 返回一个NSRange的对象。
1
NSMakeRanger (unsigned int location,unsigned int length);
例如:
1
2
NSRange range = NSMakeRanger (0 ,5 );
NSLog (@"location is %d,length is %d" ,range.location,range.length);
查找 如果找到就返回范围,否则 NSRange
的 location
项被设置为 NSNotFound
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (NSRange )rangeOfString:(NSString *)subString;
- (NSRange )rangeOfString:(NSString *)subString option:(unsigned )mask;
- (NSRange )rangeOfString:(NSString *)subString option:(unsigned )mask range:(NSRange )range;
NSString *string = @"hello world" ;
NSRange range = [string rangeOfString:@"he" ];
if (range.location != NSNotFound ) {
NSLog (@" location=%d,length=%d" ,range.location,range.length);
}
截取字符串 1
2
3
4
5
6
7
8
9
10
11
12
NSString
- (NSString *)substringToIndex:(unsigned )index;
- (NSString *)substringFromIndex:(unsigned )index;
- (NSString *)substringWithRange:(NSRange )range;
NSString *string = [string substringWithRange:NSMakeRange (5 ,2 )];
比较字符串 1
2
3
4
5
6
7
8
9
10
11
12
NSString *String1 = @"NSStringInformation.txt" ;
[String1 hasPrefix:@"NSString" ] = = 1 ? NSLog (@"YES" ) : NSLog (@"NO" );
[String1 hasSuffix:@".txt" ] = = 1 ? NSLog (@"YES" ) : NSLog (@"NO" );
if ([string1 isEqualToString:@"" ]) {
NSLog (@"string1 is blank" );
}
替换字符串 1
NSString *newString = [oldString stringByReplacingOccurrencesOfString:@"x" withString:@"y" ];
分割字符串成数组 1
2
NSString *string = @"A|B|C|D" ;
NSArray *array = [string componentsSeparatedByString:@"|" ];
读取文本文件 1
2
3
4
5
6
7
NSString
+ (id )stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
- (id )initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding )enc error:(NSError **)error
NSString *string = [NSString stringWithContentsOfFile:@"/user/test/yw.txt" encoding:NSUTF8StringEncoding error:&error];
if (string) {}
输出文本文件 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
NSString
- (BOOL )writeToFile:(NSString *)path atomically:(BOOL )useAuxiliaryFile encoding:(NSStringEncoding )enc error:(NSError **)error
The file to which to write the receiver. If path contains a tilde (~) character, you must expand it withstringByExpandingTildeInPath before invoking this method.
NSString *Path = @"~/NSData.txt" ;
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog (@"absolutePath:%@" ,absolutePath);
NSLog (@"Path:%@" ,[absolutePath stringByAbbreviatingWithTildeInPath]);
NSString *Path = @"~/NSData.txt" ;
NSLog (@"Extension:%@" ,[Path pathExtension]);