我有以下字符串
NSString *word1=@"hitoitatme";
如您所见,如果要在第二个字符后添加一个空格,则该字符串将包含最小/最大2个字符。
NSString *word2=@"hi to it at me";
我希望能够每2个字符后在字符串中添加一个白色字符空间。我将如何去做呢?因此,如果我有一个字符串,如word1,可以添加一些代码使其看起来像word2吗?我正在寻找可能的最有效方法。
先感谢您
最佳答案
在字符串中添加空格可能有不同的方法,但是一种方法可能是使用NSRegularExpression
NSString *originalString = @"hitoitatme";
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"([a-z])([a-z])" options:0 error:NULL];
NSString *newString = [regexp stringByReplacingMatchesInString:originalString options:0 range:NSMakeRange(0, originalString.length) withTemplate:@"$0 "];
NSLog(@"Changed %@", newString);//hi to it at me
关于ios - 在字符串中添加空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15582737/