【问题标题】:Replacing words with spaces in objective c在目标 c 中用空格替换单词
【发布时间】:2012-05-14 12:32:30
【问题描述】:

我有一个这样的字符串:

NSString *test = @"This is a test string";

我想用空格替换“测试”单词,如下所示:

“这是一个字符串”

我怎么能做这样的事?

我试过用这个功能

test = [test stringByReplacingOccurrencesOfString:@"test" withString:@"    "];

并没有正常工作。

【问题讨论】:

  • 您尝试该代码时给出的结果是什么?它应该使测试成为“这是一个字符串”

标签: objective-c replace nsstring


【解决方案1】:
NSString *test = @"This is a test string";
test = [test stringByReplacingOccurrencesOfString:@" test" withString:@""];
NSLog(@"%@",test);

它应该可以工作,希望它有所帮助。快乐编码:)

【讨论】:

  • 我写的代码的输出是什么?它非常适合我。
【解决方案2】:

使用 NSMutableString 代替 NSString.Repalce NSString *test = @"This is a test string" with NSMutableString *test = @"这是一个测试字符串";

【讨论】:

    【解决方案3】:
    NSString *test = @"This is a test string";
    test = [test stringByReplacingOccurrencesOfString:@"test" withString:@""];
    NSLog(@"%@",test);
    
    Use this. No need to give space. it will replace that word with empty space.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多