我在向數組中添加可變字符串(用replaceOccurrencesOfString更改過)時遇到了一些問題。這涉及到另外一個問題,我問:Remove double quotes from NSString將NSMutableString添加到NSArray
下面是代碼
//original String
NSString *aString = @"\"Hello World\"";
NSLog(@"Original String: %@",aString);
//craete mutable copy
NSMutableString *mString = [aString mutableCopy];
//remove quotes
[mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [aString length])];
NSLog(@"Mutable String: %@",mString);
//add to an array
NSArray *anArray = [NSArray arrayWithObject:mString];
NSLog(@"anArray: %@",anArray);
而結果:
2010-01-25 09:01:15.644 Quotes[20058:a0f] Original String: "Hello World"
2010-01-25 09:01:15.662 Quotes[20058:a0f] Mutable String: Hello World
2010-01-25 09:01:15.663 Quotes[20058:a0f] anArray: (
"Hello World"
)
一個 正如你所看到的,字符串被刪除的報價在mString中,但是當它添加到數組中時,它仍然有雙引號。有什麼想法嗎?
謝謝,我的腦子裏爲什麼報價仍然存在! – christo16 2010-01-25 17:20:33