我只需要从NSString的左边删除空格我的意思是
如果我" This is a good day"
,我将"This is a good day"
仅限左侧空格
请提出任何建议
答案 0 :(得分:69)
只需使用
NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
它将从左侧和右侧移除所有额外空间,但不从中间移除
并删除空格和\n
使用
NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
答案 1 :(得分:4)
使用以下内容删除NSString
。
NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
答案 2 :(得分:2)
Nsstring *str=" This is a good day";
while ([str rangeOfString:@" "].location != NSNotFound) {
str = [str stringByReplacingOccurrencesOfString:@" " withString:@" "];
}
nslog(@"string after removing space %@",)
答案 3 :(得分:0)
仅修剪左侧的解决方案是在字符串的右侧添加一个虚拟字符,修剪字符串,然后将其删除。
NSString* tempString = [NSString stringWithFormat@"%@T", yourString];
NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
return [result substringToIndex:[result length]-1];
答案 4 :(得分:0)
仅左侧空白
while ([[yourString substringToIndex:1] isEqualToString:@" "])
yourString = [yourString substringFromIndex:1];
答案 5 :(得分:-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
然后,您可以在字符串中附加 T
NSString *strT = @"T";
strString = [strT stringByAppendingString:strString];