Remove all whitespaces from NSString

后端 未结 11 1751
北荒
北荒 2020-12-04 06:23

I\'ve been trying to get rid of the white spaces in an NSString, but none of the methods I\'ve tried worked.

I have \"this is a test\" and

相关标签:
11条回答
  • 2020-12-04 07:15

    Easy task using stringByReplacingOccurrencesOfString

    NSString *search = [searchbar.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    0 讨论(0)
  • 2020-12-04 07:18

    I prefer using regex like this:

    NSString *myString = @"this is a test";
    NSString *myNewString = [myString stringByReplacingOccurrencesOfString:@"\\s"
                                         withString:@""
                                            options:NSRegularExpressionSearch
                                              range:NSMakeRange(0, [myStringlength])];
     //myNewString will be @"thisisatest"
    

    You can make yourself a category on NSString to make life even easier:

    - (NSString *) removeAllWhitespace
    {
        return [self stringByReplacingOccurrencesOfString:@"\\s" withString:@""
                                                  options:NSRegularExpressionSearch
                                                    range:NSMakeRange(0, [self length])];
    

    }

    Here is a unit test method on it too:

    - (void) testRemoveAllWhitespace
    {
        NSString *testResult = nil;
    
        NSArray *testStringsArray        = @[@""
                                             ,@"    "
                                             ,@"  basicTest    "
                                             ,@"  another Test \n"
                                             ,@"a b c d e f g"
                                             ,@"\n\tA\t\t \t \nB    \f  C \t  ,d,\ve   F\r\r\r"
                                             ,@"  landscape, portrait,     ,,,up_side-down   ;asdf;  lkjfasdf0qi4jr0213 ua;;;;af!@@##$$ %^^ & *  * ()+  +   "
                                             ];
    
        NSArray *expectedResultsArray   = @[@""
                                            ,@""
                                            ,@"basicTest"
                                            ,@"anotherTest"
                                            ,@"abcdefg"
                                            ,@"ABC,d,eF"
                                            ,@"landscape,portrait,,,,up_side-down;asdf;lkjfasdf0qi4jr0213ua;;;;af!@@##$$%^^&**()++"
                                            ];
    
        for (int i=0; i < [testStringsArray count]; i++)
        {
            testResult = [testStringsArray[i] removeAllWhitespace];
            STAssertTrue([testResult isEqualToString:expectedResultsArray[i]], @"Expected: \"%@\" to become: \"%@\", but result was \"%@\"",
                         testStringsArray[i], expectedResultsArray[i], testResult);
        }
    }
    
    0 讨论(0)
  • 2020-12-04 07:25

    This may help you if you are experiencing \u00a0 in stead of (whitespace). I had this problem when I was trying to extract Device Contact Phone Numbers. I needed to modify the phoneNumber string so it has no whitespace in it.

    NSString* yourString = [yourString stringByReplacingOccurrencesOfString:@"\u00a0" withString:@""];
    

    When yourString was the current phone number.

    0 讨论(0)
  • 2020-12-04 07:25

    Use below marco and remove the space.

    #define TRIMWHITESPACE(string) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
    

    in other file call TRIM :

        NSString *strEmail;
        strEmail = TRIM(@"     this is the test.");
    

    May it will help you...

    0 讨论(0)
  • 2020-12-04 07:29
    pStrTemp = [pStrTemp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    0 讨论(0)
提交回复
热议问题