删除 NSString 左侧的空格

发布于 2024-11-14 17:54:48 字数 129 浏览 3 评论 0原文

我只需要删除 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 day"

only the left spaces only

any suggestion please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

別甾虛僞 2024-11-21 17:54:48

只需使用

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

它即可删除左侧和右侧的所有额外空间,但不会删除中间的所有额外空间

,并删除空白和 \n 使用

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

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]];
记忆里有你的影子 2024-11-21 17:54:48

使用下面的方法从 NSString 中删除白色和换行字符。

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

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

NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
蓝天白云 2024-11-21 17:54:48
Nsstring *str="   This is a good day";

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

nslog(@"string after removing space %@",)
Nsstring *str="   This is a good day";

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

nslog(@"string after removing space %@",)
尤怨 2024-11-21 17:54:48

仅左侧空白

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

ONLY the left whitespaces

while ([[yourString substringToIndex:1] isEqualToString:@" "])
    yourString = [yourString substringFromIndex:1];
ι不睡觉的鱼゛ 2024-11-21 17:54:48

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

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

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;
}
纵情客 2024-11-21 17:54:48

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

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

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];
红墙和绿瓦 2024-11-21 17:54:48
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];
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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文