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 = @"こんにちは、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 を mac hello に置き換えたくない場合は、この方法で行うことができます

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 に答える