NSString生成对象:
变成NSString型:
置换
NSString长度取得:
int length = [@"Hello" length];
NSString内容相同的比较:
BOOL b = [@"hello" isEqualToString:@"HELLO"]; //b-->FALSE
NSString型转为其他型:(type casting/キャストする/类型转换)
int val = [@"123" intValue];
float val = [@"123.45" floatValue];
double val = [@"123.45" doubleValue];
BOOL b = [str boolValue];
取string里的某些字符:
检索string是否含有"XX":
追加string:
NSString *str = [[NSString alloc] initWithString:@"你好"];
NSString *str = [[NSString alloc] initWithFormat:@"蛋糕有%d%个",i];
//
int i = 5;
NSString *strs[] = {@"あ", @"い", @"う"};
变成NSString型:
NSString *str = @"你好";
NSString *str = [NSString stringWithString:@"你好"];
NSString *str = [NSString stringWithFormat:@"%@是%f千米。", @"距离", 42.195];
置换
%@ | |
%d | |
%f | |
%% | "%" |
NSString长度取得:
int length = [@"Hello"
NSString内容相同的比较:
BOOL b = [@"hello" isEqualToString:@"HELLO"]; //b-->FALSE
NSString型转为其他型:(type casting/キャストする/类型转换)
int val = [@"123" intValue];
float val = [@"123.45" floatValue];
double val = [@"123.45" doubleValue];
BOOL b = [str boolValue];
取string里的某些字符:
// 从头开始取到3位置前面的位置
NSString *str = [@"我爱吃蛋糕" substringToIndex:3]; //str → "我爱吃"
// 从3位置开始取到最后
NSString *str = [@"我爱吃蛋糕" substringFromIndex:3]; // str → "蛋糕"
// 从位置1开始到位置3
NSString *str = [@"我爱吃蛋糕" substringWithRange:
NSMakeRange
(1,3)]; //str → "爱吃蛋"
string是否以"XX"开头或结尾:
BOOL bl = [str hasPrefix:@"我"]; // TRUE
BOOL bl = [str hasSuffix:@"吃"]; // FALSE
去掉string的空格:
NSString *str = [@" ABC " stringByTrimmingCharacte rsInSet: [NSCharacterSetwhitespace CharacterSet]]; //str → "ABC"
检索string是否含有"XX":
NSRange searchResult = [str rangeOfString:@"AAA"];
if(searchResult.location == NSNotFound){
// 找不到的处理
}else{
// 找到的处理
}
追加string:
NSString *str1str2 = [str1 stringByAppendingString:str2];
网络url地址转成string型:NSURL *url = [NSURLURLWithString:@"http://konton.ninpou.jp/software/Tiny3D/title.dxf"];
NSString *str = [[NSString alloc] initWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error: nil];