简体   繁体   中英

Replacing occurrences of strings in Objective-C

I'm fetching data from a sqlite database in my iOS program. What I get from the DB is then placed in a UITextView . An example of text that is fetched is:

"Hello this is a example. It has "" double quotes "" and ends with simple"

I want to eliminate the single one, and replace the double by singles. But It's not working. Here is my code:

NSString *question;
question = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement1,1)] copy];   
question = [question stringByReplacingOccurrencesOfString:@"\"\"" withString:@"$$"];   
question = [question stringByReplacingOccurrencesOfString:@"\"" withString:@""];   
question = [question stringByReplacingOccurrencesOfString:@"$$" withString:@"\""];

What is wrong?

The code looks okay; I tried it by doing

NSString *question = @"\"Hello this is a example. It has \"\" double quotes \"\" and ends with simple\"";
question = [question stringByReplacingOccurrencesOfString:@"\"\"" withString:@"$$"];
question = [question stringByReplacingOccurrencesOfString:@"\"" withString:@""];
question = [question stringByReplacingOccurrencesOfString:@"$$" withString:@"\""];

and it worked fine (well, except for the fact that there are extra spaces next to where the quotes used to be, but it's hard to know what you want there)

If you add some

NSLog(@"question is now: %@", question);

lines you'll probably be able to tell what's going on or if not you'll at least have some more information to post!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM