[Objective-C] よく使う文字列操作サンプル

プログラミングを行う場合、文字列操作は必須です。
Objective-Cでよく使うサンプルをメモしておきました。
スニペットとしてお使いください。
index
文字列の長さを取得
ファイルパスから拡張子を取得
文字列連結
文字列を先頭から抜き出す
文字列の指定番から後ろを抜き出す
文字列の指定番から文字数分を抜き出す
ファイルパスからファイル名を取得
階層の追加
ファイル拡張子の追加
文字列分割
大文字英字を小文字英字に変換する
大文字英字を大文字英字に変換する
大文字英字と大文字英字を判定する
文字列比較(一致)
文字列比較(前方一致)
文字列比較(後方一致)
正規表現
正規表現(置換)
Trim処理(文字列の前後の不用スペースなどを取り除く)
参考ページ
文字列の長さを取得
1 2 3 |
MSString *str = @"test"; NSString *res = [str length]; NSLog(@"%@",res); |
結果
4
ファイルパスから拡張子を取得
1 2 |
NSString * file = @"hoge.png"; NSLog(@"Extension:%@",[file pathExtension]); |
結果
Extension:png
文字列連結
1 2 3 4 |
## AAA + BBB = AAABBB NSString *A = @"AAA"; NSString *B = @"BBB"; NSString *connect = [NSString stringWithFormat:@"%@,%@",A,B]; |
一口メモ
%@:string
%d:integer
%f:float
文字列を先頭から抜き出す
1 2 |
NSString *str = @"AbCdEfG"; NSLog(@"first:%@",[str substringToIndex:3]); |
結果
first:AbC
文字列の指定番から後ろを抜き出す
1 2 |
NSString *str = @"AbCdEfG"; NSLog(@"back:%@",[str substringFromIndex:3]); |
結果
back:dEfG
文字列の指定番から文字数分を抜き出す
1 2 |
NSString *str = @"AbCdEfG"; NSLog(@"range:%@",[str substringWithRange:NSMakeRange(1,3)]); |
結果
range:bCd
ファイルパスからファイル名を取得
1 2 |
NSString *filePath = @"hoge/currentFile.txt"; NSLog(@"FileName:%@",[filePath lastPathComponent]); |
実行結果
FileName:currentFile.txt
階層の追加
1 2 3 |
NSString *dir = @"myDirectory"; NSString *file = @"currentFile"; NSString *filePath = [dir stringByAppendingPathComponent:file]; |
実行結果
myDirectory/currentFile
ファイル拡張子の追加
1 2 |
NSString *file = @"currentFile"; NSString *file2 = [file stringByAppendingPathExtension:@".ext"]; |
実行結果
currentFile.ext
文字列分割
1 2 3 4 5 6 7 8 |
# ファイル名と拡張子に分割 NSString *fileName = "hoge.txt"; NSArray *datas = [fileName componentsSeparatedByString:@"."]; # ファイル名 NSLog(@"%@",[datas objectAtIndex:0]); # 拡張子 NSLog(@"%@",[datas objectAtIndex:1]); |
一口メモ
ファイル名以外でも、”/”を使って階層データを分割したり、URLを分割したりできます。
大文字英字を小文字英字に変換する
1 2 3 |
NSString *str = @"AbCdEfG"; NSString * res = [str lowercaseString]; NSLog(@"%@",res); |
結果
abcdefg
大文字英字を大文字英字に変換する
1 2 3 |
NSString *str = @"AbCdEfG"; NSString * res = [str upperCaseString]; NSLog(@"%@",res); |
結果
ABCDEFG
大文字英字と大文字英字を判定する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//基本文字セット NSString *str = @"AbCdEfG"; unichar c; for(int i=0;i< (int)[str length];i++){ //1文字取得 c = [str characterAtIndex:i]; //大文字判定 if(isupper(c)){ NSLog(@"Upper:%c",c); } //小文字判定 else if(islower(c)){ NSLog(@"Lower:%c",c); } } |
結果
Upper:A
Lower:b
Upper:C
Lower:d
Upper:E
Lower:f
Upper:G
文字列比較(一致)
1 2 3 4 5 6 7 8 |
NSString *str = "AbCdEfG"; NSString *str2= "abcdefg"; if([str isEqualToString:str2]){ NSLog(@"=="); } else{ NSLog(@"!="); } |
文字列比較(前方一致)
1 2 3 4 5 6 7 8 |
NSString *str = "AbCdEfG"; NSString *str2= "Ab"; if([str hasPrefix:str2]){ NSLog(@"Prefix"); } else{ NSLog(@"non-prefix"); } |
文字列比較(後方一致)
1 2 3 4 5 6 7 8 |
NSString *str = "AbCdEfG"; NSString *str2= "EfG"; if([str hasSuffix:str2]){ NSLog(@"Suffix"); } else{ NSLog(@"non-suffix"); } |
正規表現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
NSString *str = "AbCdEfG"; NSString *ptn = "^AbC"; // パターンから正規表現を生成する NSError *error = nil; NSRegularExpression *reg= [NSRegularExpression regularExpressionWithPattern:ptn options:0 error:&error]; // 正規表現を適用して結果を得る NSTextCheckingResult *match = [reg firstMatchInString:str options:0 range:NSMakeRange(0, str.length)]; // マッチ数 NSLog(@"matches: %d", match.numberOfRanges); // マッチした文字列全部 NSLog(@"%@", [str substringWithRange:[match rangeAtIndex:0]]); // 個別マッチ(1からのマッチした数の順番で取得できる) NSLog(@"%@", [str substringWithRange:[match rangeAtIndex:1]]); |
正規表現(置換)
1 2 3 4 5 6 7 8 9 10 11 12 |
NSString *str = @"AbCdEfG"; NSString *ptn = @"^AbC"; NSString *rep = @"---"; // パターンから正規表現を生成する NSError *error = nil; NSRegularExpression *reg= [NSRegularExpression regularExpressionWithPattern:ptn options:0 error:&error]; //置換処理 NSString *res = [reg stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, str.length) withTemplate:rep]; NSLog(@"reg:%@",res); |
結果
reg:—dEfG
Trim処理(文字列の前後の不用スペースなどを取り除く)
1 2 |
NSString *trimStr = @" ABC "; NSLog(@"trim:%@",[trimStr stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]); |
結果
trim:ABC
参考ページ
http://qiita.com/edo_m18/items/de7d4962e7e6d6de8ed2
http://questbeat.hatenablog.jp/entry/2014/04/30/104927
http://iphone-tora.sakura.ne.jp/nsstring.html