1

我有 NSString,如果 NSString 的内容来自 url。我想在这个 NSString 之间添加一个单词,然后是一个特定的单词。这个 NSString 是否会不断变化

例如:

我得到一个数据" hi mac this is ios",这个字符串可能会像这样不断变化 " hi ios this mac" or hi this is mac ios",等等。

我想添加一个像“hello”这样的词,后跟“mac”

" hi mac hello this is ios"
" hi ios this mac hello"
"hi this is mac hello ios"

我怎样才能做到这一点

4

4 回答 4

1

试试这个...

Yourstring = [Yourstring stringByReplacingOccurrencesOfString:@"mac" withString:@"mac hello "];
于 2013-01-03T10:54:06.197 回答
1

您可以使用 :

string = [string stringByReplacingOccurrencesOfString:@"Mac" withString:@"Mac Hello"];
于 2013-01-03T10:54:27.693 回答
0

NSString *str = @"Hi Mac 这是";

if ([str rangeOfString:@"mac" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
    int location = [str rangeOfString:@"mac" options:NSCaseInsensitiveSearch].location;

    NSMutableString *mstr = [[[NSMutableString alloc] initWithString:str] autorelease];
    NSString *strtemp = @"mac";

    [mstr replaceCharactersInRange:NSMakeRange(location, [strtemp length]) withString:[NSString stringWithFormat:@"%@ %@", strtemp, @"hello"]];

    NSLog(@"test  %@", mstr);
}
于 2013-01-03T11:15:51.237 回答
0

如果你不想用mac hello替换mac,那么你可以这样做

NSMutableString *string=[NSMutableString stringWithString:@"hi mac this is ios"];
NSRange range = [string rangeOfString:@"mac"];    
[string insertString:@" hello" atIndex:range.location+3];
NSLog(@"str=%@",string);

输出 :

str=hi mac hello this is ios
于 2013-01-03T11:16:28.483 回答