制作NSString的副本

时间:2017-03-28 05:36:30

标签: objective-c

我想在Objective C中引用其他字符串来修改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

谢谢!

1 个答案:

答案 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);