Remove whitespace from string in Objective-C

前端 未结 4 767
南旧
南旧 2020-11-30 21:59

I have a couple of strings. Some have a whitespace in the beginning and some not. I want to check if a string begins with a whitespace and if so remove it.

4条回答
  •  感情败类
    2020-11-30 22:16

    There is method for that in NSString class. Check stringByTrimmingCharactersInSet:(NSCharacterSet *)set. You should use [NSCharacterSet whitespaceCharacterSet] as parameter:

    NSString *foo = @" untrimmed string ";
    NSString *trimmed = [foo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    

提交回复
热议问题