How to remove whitespace in a string?

后端 未结 13 2153
天涯浪人
天涯浪人 2020-12-17 08:12

I have a string say \"Allentown, pa\"

How to remove the white space in between , and pa using objective c?

13条回答
  •  萌比男神i
    2020-12-17 08:42

    Here is a proper and documented way of removing white spaces from your string.

    whitespaceCharacterSet Apple Documentation for iOS says:

    Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).
    
    + (id)whitespaceCharacterSet
    Return Value
    A character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).
    
    Discussion
    This set doesn’t contain the newline or carriage return characters.
    
    Availability
    Available in iOS 2.0 and later.
    

    You can use this documented way:

    [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    

    Hope this helps you.

    If you need any more help then please let me know on this.

提交回复
热议问题