Trim spaces from end of a NSString

前端 未结 14 1198
既然无缘
既然无缘 2020-11-27 09:26

I need to remove spaces from the end of a string. How can I do that? Example: if string is \"Hello \" it must become \"Hello\"

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 09:57

    NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                                  [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //for remove whitespace and new line character
    
    NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                                  [NSCharacterSet punctuationCharacterSet]]; 
    //for remove characters in punctuation category
    

    There are many other CharacterSets. Check it yourself as per your requirement.

提交回复
热议问题