I have a string say @"DidNotTurnUp"
. I want to write a function which will take this string as an input and should output @"Did Not Turn Up"
. What would be the best way to do this? How to separate the words in a string based on capital letters? Help appreciated.
Asked
Active
Viewed 487 times
0

Kara
- 6,115
- 16
- 50
- 57

Tejas Sutar
- 747
- 2
- 11
- 33
-
Separating: http://stackoverflow.com/a/5806879/876283, Joining: http://stackoverflow.com/a/12156814/876283 – iNoob Apr 24 '14 at 06:04
-
Look for capital letters in the string and insert space before every capital letter…hope this the logic you are looking for.. – bachman Apr 24 '14 at 06:04
-
4You can use the regular expression for the same. Go through this link http://stackoverflow.com/questions/7322498/insert-or-split-string-at-uppercase-letters-objective-c – user2071152 Apr 24 '14 at 06:06
-
1@DarshanKunjadiya Please stop doing this. Your answer is pretty much visible. Reported as _not constructive_ – Desdenova Apr 24 '14 at 06:25
-
3What is this today?? Three people just copying the accepted answer of the linked-to question without any attribution. – Martin R Apr 24 '14 at 06:30
-
1So.. What do you want in the case of DOESNotTurnUp? :) – nielsbot Apr 24 '14 at 06:34
3 Answers
3
Here's a category on NSString that will do what you want.
@implementation NSString (SeparateCapitalizedWords)
-(NSString*)stringBySeparatingCapitalizedWords
{
static NSRegularExpression * __regex ;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSError * error = nil ;
__regex = [ NSRegularExpression regularExpressionWithPattern:@"[\\p{Uppercase Letter}]" options:0 error:&error ] ;
if ( error ) { @throw error ; }
});
NSString * result = [ __regex stringByReplacingMatchesInString:self options:0 range:(NSRange){ 1, self.length - 1 } withTemplate:@" $0" ] ;
return result ;
}
@end

nielsbot
- 15,922
- 4
- 48
- 73
-
1
-
-
I think the other regex might be better: any lower case letter followed by an uppercase one... but I would prefer this code structure in production. Depends how often you will be calling it. – nielsbot Apr 24 '14 at 06:43
-
1+1 for using `\p{Uppercase Letter}` which should also cover non-ASCII uppercase letters such as Ä, Ǣ, Я. – Martin R Apr 25 '14 at 06:06
-
Ok... I think this regex works better than (a-z)(A-Z), since this one will handle "IAmNewHere" --> "I Am New Here" for example. – nielsbot Apr 25 '14 at 06:43
-
`result` ends up containing the initial word uncapitalized. So if you're trying to generate a display name from, say, an obj-c property name such as `myValue`, you end up with lowercase `my`. `return [result capitalizedString]` fixes this, with final output as "My Value". – Martin-Gilles Lavoie Jul 04 '18 at 05:54
1
Try this
NSString *YourString=@"YouAreAGoodBoy";
NSString *outString;
NSString *string=@"";
NSUInteger length = [YourString length];
for (NSUInteger i = 0; i < length; i++)
{
char c = [YourString characterAtIndex:i];
if (i > 0 && c >= 65 && c <=90)
{
outString=[NSString stringWithFormat:@"%C", c];
string = [NSString stringWithFormat:@"%@%@%@",string,@" ",outString];
}
else
{
outString=[NSString stringWithFormat:@"%C", c];
string = [NSString stringWithFormat:@"%@%@",string,outString];
}
}
NSLog(@"%@",string);
Hope this code is useful for you .

Darshan Kunjadiya
- 3,323
- 1
- 29
- 31
-
I love this answer, it's original. One side note deserves mention though; in ASCII chars between 65 & 90 represent the uppercase letters hence the `if` condition. – Desdenova Apr 24 '14 at 07:05
-
"%c" works only with ASCII characters. You should use at least "%C" to make it work with UTF-16 characters (ä, €, ...). And there are uppercase letters which are not in the ASCII range such as Ä, Ǣ, Я. – Martin R Apr 25 '14 at 06:09
0
NSString *string = @"ThisStringIsJoined";
NSRegularExpression *regexp = [NSRegularExpression
regularExpressionWithPattern:@"([a-z])([A-Z])"
options:0
error:NULL];
NSString *newString = [regexp
stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, string.length)
withTemplate:@"$1 $2"];
NSLog(@"Changed '%@' -> '%@'", string, newString);
Copied from https://stackoverflow.com/a/7322720/2842382, try this