- 连接字符串
1
2
3
|
NSArray *strings = ... ;
string = [strings componentsJoinedByString: @" :-) "];
// 结果:string1:-)string2:-)...... |
- 分割字符串
1
2
|
NSString *string = @"string1:string2:string3:string4:string5";
NSArray *chunks = [string componentsSeparatedByString: @":"]; |
- 转成数值
1
2
3
4
|
NSString *string = ...;
int intValue = [string intValue]; // int数值
float floatValue = [string floatValue]; // float数值
double doubleValue = [string doubleValue]; // double数值 |
- 从字符串中去除换行
可以使用上面提到的分割字符串,然后再将它们连接起来。但是采用下面的方式更好一些。
1
2
3
4
5
6
7
8
|
NSMutableString *mstring = [NSMutableString stringWithString:string];
NSRange wholeShebang = NSMakeRange(0, [mstring length]);
[mstring replaceOccurrencesOfString: @"
"
withString: @""
options: 0
range: wholeShebang]; |
这个方法也可以用来替换字符串。这种方法比分割/连接字符串的方法稍微快一点儿,但不是很明显。处理长度1500000的字符串,当中有36909个换行,分割/连接的方法要0.124秒,这个方法要0.071秒。
- 取得子字符串
1
|
NSRange range = [[string name] rangeOfString: otherString options: NSCaseInsensitiveSearch]; |
- 取得当前日期的字符串
通常将Date转化成NSString的方法是使用NSDateFormatter,但有时候可以使用特殊方法更简单的进行转化。比如想得到:“December 4, 2007”
1
|
[[NSDate date] descriptionWithCalendarFormat: @"%B %e, %Y" timeZone: nil locale: nil] |
- Trim一个NSString
1
2
3
4
5
|
NSString *ook = @"\n \t\t hello there \t\n \n\n";
NSString *trimmed =
[ook stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// 结果是hello there |
- 按行处理一个字符串
如果一个本文文体,每一行是一条数据,想循环处理每一条数据要怎么做简单呢。看下面的例子:
1
2
3
4
5
6
7
|
NSMutableArray *altitudes = [NSMutableArray array];
NSString *altitudeString = [self altitudeStringFromGoogle: coords];
[altitudeString enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
float value = [line floatValue];
[altitudes addObject: [NSNumber numberWithFloat: value]];
}]; |
- 将字符串放在剪贴板上的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@implementation NSString (PasteboardGoodies)
- (void) sendToPasteboard
{
[[NSPasteboard generalPasteboard]
declareTypes: [NSArray arrayWithObject: NSStringPboardType]
owner:nil];
[[NSPasteboard generalPasteboard]
setString: self
forType: NSStringPboardType];
} // sendToPasteboard
@end // PasteboardGoodies |
NSString类中提供了这样三个方法用于获取子字符串:
它们该怎么使用呢?见下面代码即可知道。
NSString *str = @"12345";
// NSString *subString0 = [str substringFromIndex:-1]; //会放生越界错误
NSString *subString1 = [str substringFromIndex:0]; //@"12345"
NSString *subString2 = [str substringFromIndex:1]; //@"2345"
NSString *subString3 = [str substringFromIndex:4]; //@"5"
NSString *subString4 = [str substringFromIndex:5]; //@""
// NSString *subString5 = [str substringFromIndex:6]; //会放生越界错误
// NSString *subString10 = [str substringToIndex:-1]; //会放生越界错误
NSString *subString11 = [str substringToIndex:0]; //@"“
NSString *subString12 = [str substringToIndex:1]; //@"1”
NSString *subString13 = [str substringToIndex:4]; //@"1234“
NSString *subString14 = [str substringToIndex:5]; //@"12345”
// NSString *subString15 = [str substringToIndex:6]; //会放生越界错误
NSRange range1 = NSMakeRange(-1, 5);
NSRange range2 = NSMakeRange(0, 6);
NSRange range3 = NSMakeRange(0, 5);
NSRange range4 = NSMakeRange(1, 4);
NSRange range5 = NSMakeRange(0, 0);
NSRange range6 = NSMakeRange(4, 1);
//NSString *subString31 = [str substringWithRange:range1]; //会放生越界错误
//NSString *subString32 = [str substringWithRange:range2]; //会放生越界错误
NSString *subString33 = [str substringWithRange:range3]; //@"12345“
NSString *subString34 = [str substringWithRange:range4]; //@"2345”
NSString *subString35 = [str substringWithRange:range5]; //@"”
NSString *subString36 = [str substringWithRange:range6]; //@"5”