2017-03-28 8 views
2

Objective Cの他の文字列を参照してNSStringを変更したいのですが、誰かが助けてくれますか?NSStringのレプリカを作成する

例1:

a. "ab cd eg" 
b. "ctghml" 

I want to change string b like "ct gh ml" , in short want to insert space spaces exactly like string a. 

Note: Number of letters will be same like (abcdeg).count = 6 and (ctghml).count = 6 

例2:

a. (abcd) edt-tf 
b. (lght)ert-tg 

I want to change string b like "(lght) ert-tg" , in short want to insert space spaces exactly like string a. 

Note: Number of letters will be same without spaces like ((abcd)edt-tf).count = 12 and ((lght)ert-tg).count = 12 

ありがとう!

+0

スペース文字だけがそれらの間で違いますか? – Tj3n

+0

はい、スペースのみが違いです。 – Ren

+0

空白を追加する場所を特定する方法。文字列のスペースを追加するかどうかを決定するロジックは、正規表現を作成するために使用します。 –

答えて

0
NSString *str1 = @"ab cd eg"; 
NSMutableString *str2 = [@"ctghml" mutableCopy]; 

for(int i = 0; i < str1.length; i++) { 
    unichar c = [str1 characterAtIndex:i]; 
    if (c == ' ') { 
    [str2 insertString:@" " atIndex:i]; 
    } 
} 

NSLog(@"%@", str2); 
+0

あなたの答えをありがとう、私にチェックさせてください。 – Ren

関連する問題