2013-01-03 120 views
1

我有NSString,NSString的内容来自一个url。我想在这个NSString之间添加一个单词,然后是一个特定的单词。被这个的NSString将不断在动态NSString的中间添加一个字符串

改变,例如:

我得到像" hi mac this is ios"一个数据,这个字符串可以不断地改变这样的 " hi ios this mac" or hi this is mac ios"

我想补充像一个词“你好”,其次是 “陆委会” 总是

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

我怎么能做到这一点

+0

意味着你要加上“你好”后始终“苹果电脑”? – Exploring

+0

是的,我需要相同的 –

+0

@PradeepKumar:你可以使用insertString:atIndex:...查看我的答案。 –

回答

1

您可以使用:

string = [string stringByReplacingOccurrencesOfString:@"Mac" withString:@"Mac Hello"]; 
+0

我不想取代它,但添加到它 –

+0

雅男人,这对你的情况是一样的... – Bhavin

1

试试这个...

Yourstring = [Yourstring stringByReplacingOccurrencesOfString:@"mac" withString:@"mac hello "]; 
+0

我不想取代它,但添加到它 –

+0

@PradeepKumar:最终您的要求是满足这个代码。 –

0

的NSString *海峡= @ “嗨,苹果这是”;

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); 
} 
0

如果你不想与Mac更换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