简体   繁体   中英

Trim whitespace in between characters

i just updated to ios 7 sdk, and I would like to trim/replace the whitespace between characters of a string whereby the numbers are taken out from ABAddressBook.

I have tried using the replace " " with "" code below, but this code doesnt seems to work in ios7 sdk, it works fine in ios 6 sdk by the way.

NSString *TrimmedNumberField = [self.numberField.text 
stringByReplacingOccurrencesOfString:@" " withString:@""];

is there any other way I could do it in IOS 7?

EDIT:

It's a phone number type that I'm trying.

Input: "+65 12 345 6789"

The output i got from NSLog is " 12 345 6789"

I realized that when I added into NSDictionary and view it in NSLog, it appears that it contains a unix code representation of \  which is similar to the "dot in the middle" which is not equals to a fullstop.

thanks in advance.

Found the answer from here

phoneNumber = [phoneNumber stringByReplacingOccurencesOfString:@"." withString:@""];

// where @"." was created by typing Option + Spacebar

The number is extracted from ABAddressbook.

You can loop over the string and remove whitespace as long as there is any

NSString *someString = @"A string with   multiple spaces and    other whitespace.";

NSMutableString *mutableCopy = [someString mutableCopy];

// get first occurance of whitespace
NSRange range = [mutableCopy rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];

// If there is a match for the whitespace ...
while (range.location != NSNotFound) {
    // ... delete it
    [mutableCopy deleteCharactersInRange:range];
    // and get the next whitespace
    range = [mutableCopy rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
}

// no more whitespace. You can get back to an immutable string
someString = [mutableCopy copy];

The result with the string above is Astringwithmultiplespacesandotherwhitespace.

Try This:

NSString *str = @"   untrimmed   string   ";
NSString *trimmed = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

Try This

[yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

whitespaceCharacterSet Apple Documentation for iOS says

Returns an NSData object encoding the receiver in binary format.

  • (NSData *)bitmapRepresentation Return Value An NSData object encoding the receiver in binary format.

Discussion This format is suitable for saving to a file or otherwise transmitting or archiving.

A raw bitmap representation of a character set is a byte array of 2^16 bits (that is, 8192 bytes). The value of the bit at position n represents the presence in the character set of the character with decimal Unicode value n. To test for the presence of a character with decimal Unicode value n in a raw bitmap representation, use an expression such as the following:

So Try This

NSString *testString = @"  Eek! There are leading and trailing spaces  ";
NSString *trimmedString = [testString stringByTrimmingCharactersInSet:
                             [NSCharacterSet whitespaceAndNewlineCharacterSet]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM