msalafia
msalafia

Reputation: 2743

Comparing String ignoring spaces at the beginning or ending

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

Upvotes: 3

Views: 3982

Answers (4)

Brian Hong
Brian Hong

Reputation: 1084

For Swift 3.0+

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

Upvotes: 1

Hanny
Hanny

Reputation: 1342

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()

Upvotes: -1

Andrew Ebling
Andrew Ebling

Reputation: 10283

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

stringToTrim.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

Upvotes: 4

rishi
rishi

Reputation: 11839

 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]) {

}

Upvotes: 2

Related Questions