从NSString的第一行删除换行符 [英] Remove newline character from first line of NSString

查看:103
本文介绍了从NSString的第一行删除换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从NSString中删除第一个 \\\
字符?



编辑:为了阐明, do是:
如果字符串的第一行包含一个\\\
字符,则删除它否则不做任何操作。



ie:如果字符串是这样:

  @\ nhello,这是第一行\\\
this是第二行

,并且与第一行中不包含换行符的字符串相对:

  @hello,这是第一行\\\
this是第二行。

我希望它更清楚。

  NSString * ReplaceFirstNewLine(NSString * original) 
{
NSMutableString * newString = [NSMutableString stringWithString:original];

NSRange foundRange = [original rangeOfString:@\\\
];
if(foundRange.location!= NSNotFound)
{
[newString replaceCharactersInRange:foundRange
withString:@];
}

return [[newString retain] autorelease];
}


How can I remove the first \n character from an NSString?

Edit: Just to clarify, what I would like to do is: If the first line of the string contains a \n character, delete it else do nothing.

ie: If the string is like this:

@"\nhello, this is the first line\nthis is the second line"

and opposed to a string that does not contain a newline in the first line:

@"hello, this is the first line\nthis is the second line."

I hope that makes it more clear.

解决方案

This should do the trick:

NSString * ReplaceFirstNewLine(NSString * original)
{
    NSMutableString * newString = [NSMutableString stringWithString:original];

    NSRange foundRange = [original rangeOfString:@"\n"];
    if (foundRange.location != NSNotFound)
    {
        [newString replaceCharactersInRange:foundRange
                                 withString:@""];
    }

    return [[newString retain] autorelease];
}

这篇关于从NSString的第一行删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆