Last active
December 17, 2015 23:59
-
-
Save katsuhide/5693669 to your computer and use it in GitHub Desktop.
文字列周り
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 空のNSMutableStringを生成 | |
NSMutableString *mstr = [NSMutableString string]; | |
// 文字列を指定した生成 | |
NSString *str = @"hoge" | |
if(str != nil){ | |
NSMutableString *mstr = [NSMutableString stringWithString:str]; | |
} | |
// 文字列を追加 | |
[str1 appendString:str2]; | |
// 文字列を置換 | |
[mstr setString:@"ほげ"]; | |
// 文字列置換 | |
NSString *string =@"hogehogehoge"; // 置換対象文字列 | |
NSString *template = @"<br/>"; // 置換後文字列 | |
// 文字列置換 | |
NSString *replaced = [string stringByReplacingOccurrencesOfString:@"\n" withString:template]; | |
// 正規表現置換 | |
NSError *error = nil; | |
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"\\n" options:0 error:&error]; | |
NSString *replaced = [regexp stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0,string.length) withTemplate:template]; | |
// 文字列を結合 | |
[mstr appendString:@"hoge"]; | |
// 文字列を置換して生成 | |
NSString *str = [NSString stringWithFormat:@"hoge:%@, fuga:%d", @"hoge", 123]; | |
文字列中に%を使いたい場合は%%でエスケープする | |
// 文字列の連続した表現 | |
NSString *body = @"" | |
"hoge" | |
"fuga"; | |
// 文字列を数値に変換 | |
[str intValue]; | |
[str integerValue]; | |
[str doubleValue]; | |
// 空文字を変換した場合 | |
NSString *str = [self.source getKeyValue:@"値が取れない"]; | |
int isClassifyFlag = [str intValue]; // 0 | |
str = @""; | |
isClassifyFlag = [str intValue]; // 0 | |
str = nil; | |
isClassifyFlag = [str intValue]; // 0 | |
// NSIntegerを文字列に変換 | |
NSString *str1 = [NSString stringWithFormat:@"%d", i]; | |
NSLog(@"NSInteger→文字列:%@", str1); | |
// doubleを文字列に変換 | |
NSString *str3 = [NSString stringWithFormat:@"%f", d]; | |
NSLog(@"double→文字列:%@", str3); | |
// 指定した文字列でsplit | |
[str componentsSeparatedByString:@","] | |
// 正規表現検索 | |
NSString *string = @"red. blue. <ss type> yellow. green. blue. </ss> black. white.<ss type> hoge </ss>aaaa"; | |
NSError *error = nil; | |
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"<ss type>.+?</ss>" options:0 error:&error]; | |
if (error != nil) { | |
NSLog(@"%@", error); | |
} else { | |
NSArray *matchs = [regexp matchesInString:string options:0 range:NSMakeRange(0, string.length)]; | |
for(NSTextCheckingResult *match in matchs){ | |
int count = (int)match.numberOfRanges; | |
for(int i = 0; i < count; i++){ | |
NSLog(@"%@", [string substringWithRange:[match rangeAtIndex:i]]); | |
} | |
} | |
} | |
// 正規表現検索置換 | |
NSString *template = @""; // 置換後文字列 | |
NSString *replaced = [regexp stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0,string.length) withTemplate:template]; | |
NSLog(@"%@",string); | |
NSLog(@"%@",replaced); | |
// 文字列の比較 | |
-(BOOL)isEqualToString:(NSString*)string | |
// stringByReplacingOccurrencesOfStringを使用する場合 | |
NSString *str1 = @"woodpecker"; | |
// 文字列を置換 | |
NSString *result = [str1 stringByReplacingOccurrencesOfString:@"pecker" withString:@"stock"]; | |
NSLog(@"%@", result); | |
// replaceCharactersInRangeを使用する場合 | |
NSMutableString *str2 = [NSMutableString stringWithString:@"woodpecker"]; | |
// 文字列を置換(文字列の4番目から6文字分を置換) | |
[str2 replaceCharactersInRange:NSMakeRange(4, 6) withString:@"stock"]; | |
NSLog(@"%@", str2); | |
// 指定した文字列を含むかどうかを判定する | |
NSString *str = @"Charlie Brown"; | |
NSRange range = [str rangeOfString:@"Brown"]; | |
if (range.location != NSNotFound) { | |
NSLog(@"Brown発見"); | |
} else { | |
NSLog(@"Brownない"); | |
} | |
// 文字列を抜き出す | |
NSString *str = @"Snoopy"; | |
// 文字列の先頭から2文字を取り出す | |
NSString *substr1 = [str substringToIndex:2]; | |
// 文字列の末尾から2文字を取り出す | |
NSString *substr2 = [str substringFromIndex:2]; | |
// 指定した範囲の文字列を取り出す | |
NSString *substr3 = [str substringWithRange:NSMakeRange(3, 2)]; | |
// nilチェック | |
if(str == nil){ | |
NSLog(@"nil"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment