【问题标题】:Remove white space from the left of NSString从 NSString 的左侧删除空格
【发布时间】:2011-09-10 20:07:12
【问题描述】:

我只需要删除 NSString 左侧的空格

如果我有" This is a good day" 我将有"This is a good day"

只有左边的空格

请给点建议

【问题讨论】:

  • @Jhaliya,看看下面@AMH 需要什么 :-)
  • @Jhaliya, stringByTrimmingCharactersInSet: 这是他真正寻找的方法。在他的问题中,他问 “删除 NSString 左侧的空格”,因为他的示例中的字符串仅在左端有空格。
  • @Simon :你是对的,得到你的西蒙,我想,他有一些从左边删除的具体要求。你可以添加你的答案。
  • @Jhaliya,他的问题的答案已经在这里 :-)

标签: iphone objective-c ipad nsstring


【解决方案1】:

随便用

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

它将从左侧和右侧删除所有多余的空间,但不会从中间删除

并删除空格和\n 使用

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

【讨论】:

  • 如何用同样的方法删除\n
  • 用于删除换行符我所做的,whitespaceAndNewlineCharacterSet 不起作用,只能删除空格
【解决方案2】:

使用下面的命令从您的NSString 中删除白线和新线。

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

【讨论】:

  • @Simon:阅读我对问题的评论?
  • +1,强迫我删除我的答案。开玩笑。 +1 为正确答案:-)
【解决方案3】:
Nsstring *str="   This is a good day";

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

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

【讨论】:

  • 这是一个糟糕的解决方案,在许多其他可能的情况下都无法正常工作,例如 @"This is a good day",并且它在字符串中留下了前导空格。这是如何获得投票的??
【解决方案4】:

这是我在遇到相同问题时提出的解决方案:

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

【讨论】:

    【解决方案5】:

    对于仅向左修剪的解决方案是在字符串的右侧添加一个虚拟字符,修剪字符串,然后将其删除。

    NSString* tempString = [NSString stringWithFormat@"%@T", yourString];
    NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return [result substringToIndex:[result length]-1];
    

    【讨论】:

      【解决方案6】:

      只有左边的空格

      while ([[yourString substringToIndex:1] isEqualToString:@" "])
          yourString = [yourString substringFromIndex:1];
      

      【讨论】:

        【解决方案7】:
        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];
        

        【讨论】: