简体   繁体   English

替换Objective-C中字符串的出现

[英]Replacing occurrences of strings in Objective-C

I'm fetching data from a sqlite database in my iOS program. 我正在从iOS程序的sqlite数据库中获取数据。 What I get from the DB is then placed in a UITextView . 然后,我从数据库中获得的内容将放在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! 行中,您可能可以分辨出发生了什么,否则,至少需要发布更多信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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