Removing new line characters from NSString

前端 未结 5 1730
我寻月下人不归
我寻月下人不归 2020-12-03 00:55

I have a NSString like this:

Hello 
World
of
Twitter
Lets See this
>

I want to transform it to:

Hel

5条回答
  •  青春惊慌失措
    2020-12-03 00:57

    My case also contains \r, including \n, [NSCharacterSet newlineCharacterSet] does not work, instead, by using

    htmlContent = [htmlContent stringByReplacingOccurrencesOfString:@"[\r\n]"
                                                         withString:@""
                                                            options:NSRegularExpressionSearch
                                                              range:NSMakeRange(0, htmlContent.length)];
    

    solved my problem.

    Btw, \\s will remove all white spaces, which is not expected.

提交回复
热议问题