web-dev-qa-db-ja.com

NSStringの部分文字列の最初のインスタンスのみを置き換えます

したがって、NSStringがある場合:

@"My blue car is bigger than my blue shoes or my blue bicycle";

青の最初のインスタンスのみを緑に置き換えるメソッドを作成して、以下を生成します。

@"My green car is bigger than my blue shoes or my blue bicycle";

これはどのように行うのですか?

36
Eric Brotto

次の入力を想定します。

NSString *myString = @"My blue car is bigger then my blue shoes or my blue bicycle";
NSString *original = @"blue";
NSString *replacement = @"green";

アルゴリズムは非常に単純です。

NSRange rOriginal = [myString rangeOfString:original];

if (NSNotFound != rOriginal.location) {
    myString = [myString stringByReplacingCharactersInRange:rOriginal withString:replacement];
}
60

Swift 3および4 UPDATE:

extension String 
{
    func stringByReplacingFirstOccurrenceOfString(
            target: String, withString replaceString: String) -> String
    {
        if let range = self.range(of: target) {
            return self.replacingCharacters(in: range, with: replaceString)
        }
        return self
    } 
}
16
Kevin Sabbe

Swiftは便利ですStringはこのように拡張されます:

public extension String {

    func stringByReplacingFirstOccurrenceOfString(target: String, withString replaceString: String) -> String {
        if let range = self.rangeOfString(target) {
            return self.stringByReplacingCharactersInRange(range, withString: replaceString)
        }
        return self
    }

}

このようにして、どこからでも呼び出すことができます。

let s = "The red red cat".stringByReplacingFirstOccurrenceOfString("red", withString: "fat")
print(s) // "The fat red cat"
9
Luca Davanzo

Swift 4バージョン:

func replacingFirstOccurrence(of string: String, with replacement: String) -> String {
    guard let range = self.range(of: string) else { return self }
    return replacingCharacters(in: range, with: replacement)
}
7
Daniel Saidi
  NSString *initialString = @"My blue car is bigger then my blue shoes or my blue bicycle";
  NSRange range = [initialString rangeOfString:@"blue"];
  NSString *replacedString = [initialString stringByReplacingCharactersInRange:range withString:@"green"];
  NSLog(@"replacedString: %@", replacedString);
2
Eugene

ここに投稿された実装のより迅速なバージョン。これは、Swift 5.1の構文replacingOccurrences(of:with:)と一致します。

extension String {

    func replacingFirstOccurrence(of target: String, with replacement: String) -> String {
        guard let range = self.range(of: target) else { return self }
        return self.replacingCharacters(in: range, with: replacement)
    }
}

使用例:

let string = "I like bacon, please give me some more bacon!"
let newString = string.replacingFirstOccurrence(of: "bacon", with: "meat")
2
ullstrm
-(NSString*) replaceFirstOccuarnceFromString: (NSString*)input withOriginal:(NSString*) original AndReplacment:(NSString*)replacement
{
    NSRange rOriginal = [input rangeOfString: original];
    if (NSNotFound != rOriginal.location) {
        input = [input
                    stringByReplacingCharactersInRange: rOriginal
                    withString:                         replacement];
    }
    return input;
}
0
Abo3atef

最初に部分文字列を見つけ、次に置換を行います。例:

NSString *aString = @"foo bar foo";
NSRange firstFooRange = [aString rangeOfString:@"foo"];
NSString *anotherString = [aString stringByReplacingOccurrencesOfString:@"foo"
                                                             withString:@"bar"
                                                                options:0
                                                                  range:firstFooRange];

NSString ドキュメント。

0
user971401