16

I need to remove the white space only from the left of NSString I mean

if I have " This is a good day" I will have "This is a good day"

only the left spaces only

any suggestion please

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
AMH
  • 6,363
  • 27
  • 84
  • 135

7 Answers7

70

Just use

NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

it will remove all extra space from left as well as right but not from middle

and to remove both white space and \n use

NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Amit Singh
  • 8,383
  • 4
  • 28
  • 31
4

Use below to remove white and new line chatacter from your NSString.

NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
2
Nsstring *str="   This is a good day";

  while ([str rangeOfString:@"  "].location != NSNotFound) {
    str = [str stringByReplacingOccurrencesOfString:@"  " withString:@" "];
}

nslog(@"string after removing space %@",)
sinh99
  • 3,909
  • 32
  • 32
  • This is a terrible solution that does not work correctly for many other possible cases, such as @" This is a good day", and it leaves a leading space in the string. How did this get upvoted?? – Thomas Tempelmann Mar 07 '18 at 15:02
0

Here's a solution I came up with when facing the same issue:

- (NSString*)trimBlankCharactersFromBeginningOfString:(NSString*)originalString
{
    //we are only trimming the characters on the left side of the string
    NSInteger count = [originalString length];
    NSString *trimmedNewText = originalString;
    for (int i = 0; i < count; i++) {
        if([[originalString substringWithRange:NSMakeRange(i, 1)] rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location == NSNotFound)
        {
            trimmedNewText = [originalString substringFromIndex:i];
            break;
        }
        else
        {
            if(i+1 < count)
            {
                trimmedNewText = [originalString substringFromIndex:i+1];
            }
            //since we are exiting the loop as soon as a non blank space character is found
            //then this is just really checking to see if we are on the last character of an
            //all whitespace string. can't say i+1 here because that would give an index out
            //of bound exception
            else
            {
                trimmedNewText = @"";
            }
        }
    }
    return trimmedNewText;
}
Brian Sachetta
  • 3,319
  • 2
  • 34
  • 45
0

For trimming only to the left a solution is to add a dummy character to the right of your string, trim the string, and then remove it.

NSString* tempString = [NSString stringWithFormat@"%@T", yourString];
NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
return [result substringToIndex:[result length]-1];
vib
  • 2,254
  • 2
  • 18
  • 36
0

ONLY the left whitespaces

while ([[yourString substringToIndex:1] isEqualToString:@" "])
    yourString = [yourString substringFromIndex:1];
quarac
  • 3,454
  • 3
  • 21
  • 25
-1
NSString *str = @" This is a good day";    
NSArray *arrString = [str componentsSeparatedByString:@"T"];
NSString *strSpace = [arrString objectAtIndex:0];   // White space
NSString *strString = [arrString objectAtIndex:1];   //This is a good day  

You can then append T in the string as

NSString *strT = @"T";
strString = [strT stringByAppendingString:strString];
Nitish
  • 13,845
  • 28
  • 135
  • 263