1

I want to remove only first space in below string.

NSString *str = @"IF_Distance (GET_Mi mi=km*1.4,STRING1,STRING2)";

Note: There is a space after IF_Distance and another space after GET_Mi. I am unable to remove the space after IF_Distance.

Anand Kore
  • 1,300
  • 1
  • 15
  • 33
  • 1
    Possible duplicate of [Replace only the first instance of a substring in an NSString](https://stackoverflow.com/questions/8608346/replace-only-the-first-instance-of-a-substring-in-an-nsstring) – adev Aug 02 '17 at 05:29
  • Check the answer in that question. – adev Aug 02 '17 at 05:29
  • 1
    *I want to remove only first space in below string.* The solution is obvious and quite simple: `str = @"IF_Distance(GET_Mi mi=km*1.4,STRING1,STRING2)"` ;-) – Amin Negm-Awad Aug 02 '17 at 05:48

5 Answers5

2

Use rangeOfString: to locate the first space, then use stringByReplacingCharactersInRange:withString: to replace it with the empty string.

CRD
  • 52,522
  • 5
  • 70
  • 86
0

Remove space by using below code.

NSString *str = @"IF_Distance (GET_Mi mi=km*1.4,STRING1,STRING2)";

NSString *secondString = [str stringByReplacingOccurrencesOfString:@"IF_Distance " withString:@"IF_Distance"];
Neha Gupta
  • 539
  • 3
  • 13
0

Try This:

NSString *str = @"IF_Distance (GET_Mi mi=km*1.4,STRING1,STRING2)";
NSString *firstStringContainingSpace = [[str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] firstObject];//firstStringContainingSpace = IF_Distance

str = [str stringByReplacingCharactersInRange:[str rangeOfString:[NSString stringWithFormat:@"%@ ",firstStringContainingSpace]] withString:firstStringContainingSpace];

Output: str = @"IF_Distance(GET_Mi mi=km*1.4,STRING1,STRING2)";

Torongo
  • 1,021
  • 1
  • 7
  • 14
  • `[NSString stringWithFormat:@"%@ ",firstStringContainingSpace]` is pointless. Moreover, separating the whole string by whitespaces while only the first one is relevant, is pointless, too. – Amin Negm-Awad Aug 02 '17 at 05:53
-1

You can remove first space by using following code:

First find space by using rangeOfString: and then remove by using stringByReplacingCharactersInRange:withString: method.

Like,

NSString *str = @"IF_Distance (GET_Mi mi=km*1.4,STRING1,STRING2)";
NSString *strSpace = @" ";

NSRange range = [str rangeOfString:strSpace];
NSString *strFinal;

if (NSNotFound != range.location) {

     strFinal = [str stringByReplacingCharactersInRange:range withString:@""];
}
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
-1

If you are looking for some more universal way - this is the variant of it:

- (NSString *)removeWhitespaces:(NSString *)string {

    NSMutableArray * stringComponents = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy];
    NSString * fStringComponent = [stringComponents firstObject];
    [stringComponents removeObject:fStringComponent];

    return [fStringComponent stringByAppendingString:[stringComponents componentsJoinedByString:@" "]];
}
Nazir
  • 1,945
  • 24
  • 27