首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复制NSString

复制NSString
EN

Stack Overflow用户
提问于 2017-03-28 05:36:29
回答 1查看 74关注 0票数 2

我想参照目标C中的其他字符串修改一个NSString,有人能帮我吗?

Example1:

代码语言:javascript
复制
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

Example2:

代码语言:javascript
复制
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

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-28 05:56:11

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43061356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档