32

I have a string say "Allentown, pa"

How to remove the white space in between , and pa using objective c?

wim
  • 338,267
  • 99
  • 616
  • 750
Maga
  • 329
  • 1
  • 3
  • 3

13 Answers13

66

This will remove all space from myString.

NSString *newString = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • @VanDuTran this was written long ago. I'm not working with iOS for few years, so can't help with iOS 9 right now. Sorry. – taskinoor Oct 15 '15 at 17:27
  • No prob, I just wanted to point that out. It doesn't work for " " space character. – Van Du Tran Oct 15 '15 at 17:30
  • 1
    it does not work in iOS9. Apple just bended the rules without anyone knowing it? Such a basic function.. How many devs will suffer this change since iOS9? – Yaro Jun 07 '16 at 20:54
31

Here is a proper and documented way of removing white spaces from your string.

whitespaceCharacterSet Apple Documentation for iOS says:

Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).

+ (id)whitespaceCharacterSet
Return Value
A character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).

Discussion
This set doesn’t contain the newline or carriage return characters.

Availability
Available in iOS 2.0 and later.

You can use this documented way:

[yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

Hope this helps you.

If you need any more help then please let me know on this.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • 17
    Just to note: this only trims whitespace at the beginning and end of a string, not in the middle. – Dick Oct 23 '12 at 17:55
  • 4
    This doesn't answer the question - it may be correct for trimming characters and the beginning and end, but it doesn't do what the OP wants which is remove white space from inside the string. – siburb Dec 12 '13 at 07:16
  • 7
    This would be the shortest hack: `[[str componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceCharacterSet] componentsJoinedByString:@""]` – Fjölnir Jun 26 '15 at 05:34
  • @Fjölnir: You are correct. I was a novice back then. Didn't know details about it then. – Parth Bhatt Jun 26 '15 at 05:35
  • 1
    Fjölnir's little comment here should be the accepted answer! :-) Thanks! – Erik van der Neut May 26 '16 at 04:31
12

Probably the solution in one of the answers in Collapse sequences of white space into a single character and trim string:

NSString *whitespaceString = @" String with whitespaces ";

NSString *trimmedString = [whitespaceString stringByReplacingOccurrencesOfString:@" " withString:@""];
Community
  • 1
  • 1
gw0
  • 1,533
  • 2
  • 12
  • 13
9

If you want to white-space and new-line character as well then use "whitespaceAndNewlineCharacterSet" instead of "whitespaceCharacterSet"

 NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
 NSString *trimmedString = [temp.text stringByTrimmingCharactersInSet:whitespace];

 NSLog(@"Value of the text field is %@",trimmedString);   
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
2
myStr = [myStr stringByReplacingOccurrencesOfString:@" " withString:@""];
EdoDodo
  • 8,220
  • 3
  • 24
  • 30
1
NSString *sample = @" string with whitespaces";
NSString *escapeWhiteSpaces = [sample stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
JIthin
  • 1,413
  • 1
  • 13
  • 29
1
- (NSString *)removeWhitespaces {
  return [[self componentsSeparatedByCharactersInSet:
                    [NSCharacterSet whitespaceCharacterSet]]
      componentsJoinedByString:@""];
}
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Aqib Mumtaz
  • 4,936
  • 1
  • 36
  • 33
  • 1
    It didn't have proper code formatting and indentation for better visibility. Whole code was displayed in one line. – Parth Bhatt Jun 26 '15 at 06:31
1

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.

Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68
1

Here is the proper way to remove extra whitespaces from string which is coming in between.

NSString *yourString = @"Allentown,   pa";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];

NSArray *parts = [yourString componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
yourString = [filteredArray componentsJoinedByString:@" "];
Brijesh Singh
  • 739
  • 1
  • 6
  • 23
0

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.

semicircle21
  • 741
  • 8
  • 12
  • @Rambatino No. I was sending the string to a remote server and found it from the server log. I thought it also can be seen during the Xcode's debugging mode. – semicircle21 Sep 25 '14 at 12:32
0

Hi there is the swift version of the solution with extension :

extension String{
    func deleteSpaces() -> String{
        return self.stringByReplacingOccurrencesOfString(" ", withString: "")
    }
}

And Just call

(yourString as! String).deleteSpaces()
Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99
0

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

- (NSString*)remove:(NSString*)textToRemove fromString:(NSString*)input { 
    return [input stringByReplacingOccurrencesOfString:textToRemove withString:@""]; 
}
taskinoor
  • 45,586
  • 12
  • 116
  • 142
Muhammad Usama
  • 2,797
  • 1
  • 17
  • 14
-3

Swift 3:

var word: String = "Hello world"
let removeWhiteSpace = word.stringByRemovingWhitespaces
word = "Helloworld"
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98