3

I'm new with iOS developing and i'm looking for a solution to compare two String ignoring the spaces at the beginning or at the ending. For example, "Hello" == "Hello " should return true.

I've serached for a solution but i didn't find nothing in Swift. Thanks

msalafia
  • 2,703
  • 5
  • 23
  • 34
  • 1
    Consider trimming the string. See: http://stackoverflow.com/questions/5756256/objective-c-trim-spaces-from-end-of-a-string – Jack Jul 07 '14 at 17:02
  • 1
    Here's a link to a string reference in Swift: http://www.learnswiftonline.com/reference-guides/string-reference-guide-for-swift/. Search that page for "trim". – Blake Schwendiman Jul 07 '14 at 17:41

4 Answers4

5

I would recommend you start by trimming whitespace from the String with this Swift code:

stringToTrim.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • This is the best solution - whitespace is trimmed only from beginning and end, not from the middle of a string. " Roger Rabbit " becomes "Roger Rabbit". – Adam Eberbach Jun 22 '16 at 00:59
2
 NSString *string1 = @" Hello";
 //remove(trim) whitespaces
 string1 = [string1 stringByReplacingOccurrencesOfString:@" " withString:@""];

 NSString *string2 = @"Hello ";
 //remove(trim) whitespaces
 string2 = [string1 stringByReplacingOccurrencesOfString:@" " withString:@""]

 // compare strings without whitespaces
 if ([string1 isEuqalToString:string2]) {
 }

So if you want to use it directly -

if ([[yourString1 stringByReplacingOccurrencesOfString:@" " withString:@""] isEuqalToString:[yourString2 stringByReplacingOccurrencesOfString:@" " withString:@""]]) {
// Strings are compared without whitespaces.
}

Above will remove all whitespaces of your string, if you want to remove only leading and trailing whitespaces then there are a couple of post already available, you can create a category of string as mentioned in following stack overflow post - How to remove whitespace from right end of NSString?

@implementation NSString (TrimmingAdditions)

- (NSString *)stringByTrimmingLeadingCharactersInSet:(NSCharacterSet *)characterSet {
    NSUInteger location = 0;
    NSUInteger length = [self length];
    unichar charBuffer[length];    
    [self getCharacters:charBuffer];

    for (location; location < length; location++) {
        if (![characterSet characterIsMember:charBuffer[location]]) {
            break;
        }
    }

    return [self substringWithRange:NSMakeRange(location, length - location)];
}

- (NSString *)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet {
    NSUInteger location = 0;
    NSUInteger length = [self length];
    unichar charBuffer[length];    
    [self getCharacters:charBuffer];

    for (length; length > 0; length--) {
        if (![characterSet characterIsMember:charBuffer[length - 1]]) {
            break;
        }
    }

    return [self substringWithRange:NSMakeRange(location, length - location)];
}

@end

Now once you have the methods available, you can call these on your strings to trim leading and trailing spaces like -

 // trim leading chars
 yourString1 = [yourString1 stringByTrimmingLeadingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// trim trainling chars
yourString1 = [yourString1 stringByTrimmingTrailingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

// trim leading chars
 yourString2 = [yourString2 stringByTrimmingLeadingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// trim trainling chars
yourString2 = [yourString2 stringByTrimmingTrailingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

// compare strings
if([yourString1 isEqualToString: yourString2]) {

}
Community
  • 1
  • 1
rishi
  • 11,779
  • 4
  • 40
  • 59
  • 4
    This removes spaces in the middle of the string, you should use `stringByTrimmingCharactersInSet` to remove only leading and trailing spaces. Also...this isn't even in Swift... – Jack Jul 07 '14 at 17:03
  • 1
    Though this is the accepted answer, it's not Swift. If you're here... and are looking for Swift... keep reading! – EricWasTaken May 22 '19 at 23:26
1

For Swift 3.0+

Use .trimmingCharacters(in: .whitespaces) before comparison or .trimmingCharacters(in: .whitespacesAndNewlines)

Brian Hong
  • 930
  • 11
  • 15
-1

In Swift 4
Use it on any String type variable.

extension String {
    func trimWhiteSpaces() -> String {
        let whiteSpaceSet = NSCharacterSet.whitespaces
        return self.trimmingCharacters(in: whiteSpaceSet)
    }
}

And Call it like this

yourString.trimWhiteSpaces()
Hanny
  • 1,322
  • 15
  • 20