6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NSString 比較系メソッド覚書

Last updated at Posted at 2015-11-26

お馴染みのメソッド

完全一致

NSString.h
- (BOOL)isEqualToString:(NSString *)aString;
- (NSComparisonResult)compare:(NSString *)string;

上記2つのメソッドは若干挙動が異なる。

NSStringの文字列比較はisEqualToStringではなくてcompareメソッドがいいのか
http://iphone-dev.g.hatena.ne.jp/ktakayama/20130809/1376034733

前方一致

NSString.h
- (BOOL)hasPrefix:(NSString *)str;

後方一致

NSString.h
- (BOOL)hasSuffix:(NSString *)str;

比較的新しいメソッド

部分一致(大文字・小文字を区別する)

NSString.h
- (BOOL)containsString:(NSString *)str NS_AVAILABLE(10_10, 8_0);

従来の書き方

if ([stringA rangeOfString:stringB].location != NSNotFound) {
    NSLog(@“一致");
}

部分一致(大文字・小文字を区別しない)

NSString.h
- (BOOL)localizedCaseInsensitiveContainsString:(NSString *)str NS_AVAILABLE(10_10, 8_0);

従来の書き方

if ([stringA rangeOfString:stringB options:NSCaseInsensitiveSearch].location != NSNotFound) {
    NSLog(@“一致");
}

部分一致(正規化)

NSString.h
- (BOOL)localizedStandardContainsString:(NSString *)str NS_AVAILABLE(10_11, 9_0);

ロケールが考慮され、大文字・小文字や発音記号が正規化される。
以下の2例はいずれも一致となる。

if ([@"a" localizedStandardContainsString:@"á"]) {
    NSLog(@"一致");
}

if ([@"a" localizedStandardContainsString:@"A"]) {
    NSLog(@"一致");
}
6
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?