[英]How can I change substring(s) in an array of strings in Objective-C?
I have a weird error with decoding &
sign so its always shown as &
我在解码&
签名时遇到了一个奇怪的错误,所以它总是显示为&
, so &&
will be &&
, 所以&&
将是&&
, a&b
is shown as a&b
etc. , a&b
显示为a&b
等。
I have those weird characters in my array of strings sometimes, so I want to remove them.有时我的字符串数组中有那些奇怪的字符,所以我想删除它们。 To be more clear, if I have an array like this:更清楚地说,如果我有一个这样的数组:
"str1", "a&b", "12345", "d&&emo&"
I want to it to be:我希望它是:
"str1", "a&b", "12345", "d&&emo&"
So all &
所以所有&
should be changed into just &
.应该改为&
。
This is the code I tried, but it didn't help.这是我尝试过的代码,但没有帮助。 Elements are not changing their values:元素不会改变它们的值:
for (int i = 0; i <= self.array-1; i++)
{
NSLog(@"%@", self.array[i]);
[self.array[i] stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSLog(@"%@", self.array[i]);
}
I am new to Objective-C.我是 Objective-C 的新手。
NSString
instances are immutable, which means you can't change them once they are created. NSString
实例是不可变的,这意味着一旦创建它们就不能更改它们。 The stringByReplacingOccurencesOfString:withString:
method actually returns a new NSString
instance. stringByReplacingOccurencesOfString:withString:
方法实际上返回一个新的NSString
实例。 You could use NSMutableString
, or alternatively, you can create a new array containing the replacement strings, eg:您可以使用NSMutableString
,或者,您可以创建一个包含替换字符串的新数组,例如:
NSMutableArray *newArray = [NSMutableArray array];
for (NSString *input in self.array)
{
NSString *replacement = [input stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
[newArray addObject:replacement];
}
self.array = newArray;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.