Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove white space from the left of NSString

I need to remove the white space only from the left of NSString I mean

if I have " This is a good day" I will have "This is a good day"

only the left spaces only

any suggestion please

like image 466
AMH Avatar asked Jun 08 '11 12:06

AMH


3 Answers

Just use

NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

it will remove all extra space from left as well as right but not from middle

and to remove both white space and \n use

NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
like image 128
Amit Singh Avatar answered Oct 05 '22 22:10

Amit Singh


Use below to remove white and new line chatacter from your NSString.

NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
like image 25
Jhaliya - Praveen Sharma Avatar answered Oct 05 '22 22:10

Jhaliya - Praveen Sharma


Nsstring *str="   This is a good day";

  while ([str rangeOfString:@"  "].location != NSNotFound) {
    str = [str stringByReplacingOccurrencesOfString:@"  " withString:@" "];
}

nslog(@"string after removing space %@",)
like image 33
sinh99 Avatar answered Oct 05 '22 22:10

sinh99