我想在两个单词之间添加一个间距,如果它已经不在那里:
//Sample 1
NSString *word1 = @"First";
NSString *word2 = @"Word";
NSString *output = [NSString stringWithFormat:@"%@%@", word1, word2];
//output = FirstWord --> I want "First Word"
如果已经有“First”空格,则不应再添加另一个空格。
答案 0 :(得分:0)
修剪第一个字符串,然后总是放一个空格。
NSString *word1 = @"First";
NSString *word2 = @"Word";
NSString *word1Trimmed = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
NSString *output = [NSString stringWithFormat:@"%@ %@", word1Trimmed, word2];