开发者

Remove white space from the left of NSString

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 d开发者_JAVA百科ay"

only the left spaces only

any suggestion please


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]];


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

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


Nsstring *str="   This is a good day";

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

nslog(@"string after removing space %@",)


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;
}


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];


ONLY the left whitespaces

while ([[yourString substringToIndex:1] isEqualToString:@" "])
    yourString = [yourString substringFromIndex: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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜