如何将多个字符串替换为多个其他字符串[NSString]

时间:2021-05-29 07:50:46
NSString *string  = [myString stringByReplacingOccurrencesOfString:@"<wow>" withString:someString];

I have this code. Now suppose my app's user enters two different strings I want to replace with two different other strings, how do I achieve that? I don't care if it uses private APIs, i'm developing for the jailbroken platform. My user is going to either enter or or . I want to replace any occurrences of those strings with their respective to-be-replaced-with strings :)

我有这段代码。假设我的app用户输入两个不同的字符串我想用两个不同的字符串替换,我如何实现呢?我不关心它是否使用私有api,我正在开发一个越狱平台。我的用户要么输入要么。我想用它们各自的要替换的字符串替换这些字符串的出现:)

Thanks in advance :P

提前谢谢:P

4 个解决方案

#1


6  

Both dasblinkenlight’s and Matthias’s answers will work, but they both result in the creation of a couple of intermediate NSStrings; that’s not really a problem if you’re not doing this operation often, but a better approach would look like this.

dasblinkenlight和Matthias的答案都是可行的,但它们都导致了两个中间的nsstring;如果你不经常做这个操作,这并不是真正的问题,但是更好的方法应该是这样的。

NSMutableString *myStringMut = [[myString mutableCopy] autorelease];
[myStringMut replaceOccurrencesOfString:@"a" withString:somethingElse];
[myStringMut replaceOccurrencesOfString:@"b" withString:somethingElseElse];
// etc.

You can then use myStringMut as you would’ve used myString, since NSMutableString is an NSString subclass.

然后可以像使用myString那样使用myStringMut,因为NSMutableString是一个NSString子类。

#2


6  

The simplest solution is running stringByReplacingOccurrencesOfString twice:

最简单的解决方案是运行stringByReplacingOccurrencesOfString两次:

NSString *string  = [[myString
    stringByReplacingOccurrencesOfString:@"<wow>" withString:someString1]
    stringByReplacingOccurrencesOfString:@"<boo>" withString:someString2];

#3


2  

I would just run the string replacing method again

我将再次运行字符串替换方法

NSString *string  = [myString stringByReplacingOccurrencesOfString:@"foo" withString:@"String 1"];
string = [string stringByReplacingOccurrencesOfString:@"bar" withString:@"String 2"];

#4


2  

This works well for me in Swift 3.1

在Swift 3.1中,这对我来说很适用

let str = "hi hello hey"

var replacedStr = (str as NSString).replacingOccurrences(of: "hi", with: "Hi")

replacedStr = (replacedStr as NSString).replacingOccurrences(of: "hello", with: "Hello")

replacedStr = (replacedStr as NSString).replacingOccurrences(of: "hey", with: "Hey")

print(replacedStr) // Hi Hello Hey

#1


6  

Both dasblinkenlight’s and Matthias’s answers will work, but they both result in the creation of a couple of intermediate NSStrings; that’s not really a problem if you’re not doing this operation often, but a better approach would look like this.

dasblinkenlight和Matthias的答案都是可行的,但它们都导致了两个中间的nsstring;如果你不经常做这个操作,这并不是真正的问题,但是更好的方法应该是这样的。

NSMutableString *myStringMut = [[myString mutableCopy] autorelease];
[myStringMut replaceOccurrencesOfString:@"a" withString:somethingElse];
[myStringMut replaceOccurrencesOfString:@"b" withString:somethingElseElse];
// etc.

You can then use myStringMut as you would’ve used myString, since NSMutableString is an NSString subclass.

然后可以像使用myString那样使用myStringMut,因为NSMutableString是一个NSString子类。

#2


6  

The simplest solution is running stringByReplacingOccurrencesOfString twice:

最简单的解决方案是运行stringByReplacingOccurrencesOfString两次:

NSString *string  = [[myString
    stringByReplacingOccurrencesOfString:@"<wow>" withString:someString1]
    stringByReplacingOccurrencesOfString:@"<boo>" withString:someString2];

#3


2  

I would just run the string replacing method again

我将再次运行字符串替换方法

NSString *string  = [myString stringByReplacingOccurrencesOfString:@"foo" withString:@"String 1"];
string = [string stringByReplacingOccurrencesOfString:@"bar" withString:@"String 2"];

#4


2  

This works well for me in Swift 3.1

在Swift 3.1中,这对我来说很适用

let str = "hi hello hey"

var replacedStr = (str as NSString).replacingOccurrences(of: "hi", with: "Hi")

replacedStr = (replacedStr as NSString).replacingOccurrences(of: "hello", with: "Hello")

replacedStr = (replacedStr as NSString).replacingOccurrences(of: "hey", with: "Hey")

print(replacedStr) // Hi Hello Hey