I want to separate a string with the occurrence of the Uppercase letters. Suppose I have an string like "IAmABoy".I want the resulting strings like
I
Am
A
Boy.
How to do this? Is there any sample code? Please show me the way to do this.
I want to separate a string with the occurrence of the Uppercase letters. Suppose I have an string like "IAmABoy".I want the resulting strings like
I
Am
A
Boy.
How to do this? Is there any sample code? Please show me the way to do this.
I'd suggest importing a Regex library or taking a look at NSScanner.
Try using -rangeOfCharacterFromSet: and splitting the string based on where you find the upper-case characters.
Thanx Graham Lee for your reply.Can you provide me some sample codes.
What about something simple like just iterating over each character? Assuming inputString and outputString are NSStrings something like this should work:
NSUInteger length = [inputString length];
for (NSUInteger i = 0; i < length; i++)
{
char c = [inputString characterAtIndex:i];
if (i > 0 && c >= 65 && c <=90)
{
[outputString appendFormat:@"\n%c", c];
}
else
{
[outputString appendFormat:@"%c", c];
}
}
Thanx Carson for you reply.But the line
[outputString appendFormat:@"\n%c", c];
througing some exception.I have modify your code to the following one and it seems to be working for me.
NSString *inputString=@"IAmABoy";
NSString *outputString;
NSString *string=@"";
NSUInteger length = [inputString length];
for (NSUInteger i = 0; i < length; i++)
{
char c = [inputString characterAtIndex:i];
if (i > 0 && c >= 65 && c <=90)
{
//[outputString appandFormate:@"\n%c", c];
outputString=[NSString stringWithFormat:@"%c", c];
string = [NSString stringWithFormat:@"%@%@%@",string,@" ",outputString];
NSLog(@"%@", string);
}
else
{
//[outputString appendFormat:@"%c", c];
outputString=[NSString stringWithFormat:@"%c", c];
//NSLog(@"%@",outputString);
string = [NSString stringWithFormat:@"%@%@",string,outputString];
NSLog(@"%@", string);
}
}
Thanx once again to all for your cooperations and help.