IOS string to string handling (removing Spaces)

  question

Problem description

In the project,

  1. Some textfields require no Spaces at the end of the string.
  2. There are some strings that cannot have Spaces in them.
  3. Enter only letters and Numbers (control TextField to accept only characters within display control)

To solve the above situations one by one:

Solutions:

  1. Remove Spaces at the beginning and end of the string:

OC implementation:

Use of nsstrings stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] method only removes white Spaces at the left and right sides of;

By OC:

NSString *str  = [NSString stringWithFormat:@"    sda  sda    "];
str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
NSLog(@"%@", str);

By Swift:

var str = "    sda  sad    "
str = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
print(str)

Remove All Spaces:

NSString *str = [NSString stringWithFormat:@"   what the  f**k are you doing !  "];
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"%@", str);

LEAVE A COMMENT