How to remove whitespace in a string?

后端 未结 13 2135
天涯浪人
天涯浪人 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条回答
  • Swift 3:

    var word: String = "Hello world"
    let removeWhiteSpace = word.stringByRemovingWhitespaces
    word = "Helloworld"
    
    0 讨论(0)
  • 2020-12-17 08:32
    myStr = [myStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    0 讨论(0)
  • 2020-12-17 08:32

    In my case NSString was added Zero Width Space(i i used some library). so solution worked for me.

    NSMutableString *newString=[[newString stringByReplacingOccurrencesOfString:@"\u200B" withString:@""] mutableCopy];
    

    @"\u200B" is Zero width space character value.

    0 讨论(0)
  • 2020-12-17 08:32

    you can use remove function to remove any substring from the string

    - (NSString*)remove:(NSString*)textToRemove fromString:(NSString*)input { 
        return [input stringByReplacingOccurrencesOfString:textToRemove withString:@""]; 
    }
    
    0 讨论(0)
  • 2020-12-17 08:34

    This will remove all space from myString.

    NSString *newString = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    0 讨论(0)
  • 2020-12-17 08:37

    I have tried all the solutions here, none of them could remove the whitespace generated by the Chinese PinYin Input method.

    After some debugging, I found this working:

    NSString *newString = [myString stringByReplacingOccurrencesOfString:@"\342\200\206" withString:@""];
    

    I have googled what the '\342\200\206' is, but failed.

    Whatever, it works for me.

    0 讨论(0)
提交回复
热议问题