-1

I want to string string from the end of string, is there any api function of string which ONLY removes Space and Newline from END of string.

I wrote manual code to search character from end of string and remove space and newline but it may slow the process.

API function needed..

Thanks in advance

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Ajay_Kumar
  • 1,381
  • 10
  • 34
  • 62
  • Does your string have spaces in-between words? `hello hello ` vs `hello `? – nattyddubbs Mar 14 '13 at 12:51
  • @nattyddubbs , yes my string is containing space and newline in words, its kind of big sentence but I want to remove space and newline from the end only. – Ajay_Kumar Mar 14 '13 at 12:53

1 Answers1

0

try this one may be it helps you,

-(NSString *)removeEndSpaceFrom:(NSString *)strtoremove{
    NSUInteger location = 0;
    unichar charBuffer[[strtoremove length]];
    [strtoremove getCharacters:charBuffer];
    int i = 0;
    for ( i = [strtoremove length]; i >0; i--){
        if (![[NSCharacterSet whitespaceCharacterSet] characterIsMember:charBuffer[i - 1]]){
            break;
        }
    }
    return  [strtoremove substringWithRange:NSMakeRange(location, i  - location)];
}

and

 NSString *string = @"          this text has spaces before and after         ";
    NSString *trimmedString = [self removeEndSpaceFrom:string];
    NSLog(@"%@",trimmedString);
Balu
  • 8,470
  • 2
  • 24
  • 41