0

在我的应用程序中,我正在读取一个 HTML 页面到一个字符串。HTML 页面包含许多标签、换行符和许多其他字符。

<style type="text/css">
    h3{ 
        font-weight:bold;
        line-height:18px;
        font-family:Arial;
        font-size:12px; 
        text-align:justify; 
        color:black; 
        background-color:transparent;
        width:280px;
        margin:0;
    }

    body{ 
        font-weight:normal;
        line-height:18px;
        font-family:Arial;
        font-size:12px; 
        text-align:justify; 
        color:black; 
        background-color:transparent;
        width:280px;
    }
</style>

我想像下面这样替换这个字符串。当我们 NSLog 上面的 HTML 页面时,它会打印如上所示。但我想要的 NSLog 如下

\t\t<style type="text/css">\n\t\t\th3{\n
        font-weight:bold;
        line-height:18px;
        font-family:Arial;
        font-size:12px; 
        text-align:justify; 
        color:black; 
        background-color:transparent;
        width:280px;
        margin:0;
    }

    body{ 
        font-weight:normal;
        line-height:18px;
        font-family:Arial;
        font-size:12px; 
        text-align:justify; 
        color:black; 
        background-color:transparent;
        width:280px;
    }
</style>

我的意思是包括反斜杠字符。是否可以?这背后的原因 - 我想动态替换上面的样式 - 但是为了替换上面我必须有源来替换 - 如何获取包含 \n&\r 字符的源?

4

1 回答 1

4

查看NSString 参考。你需要类似的东西:

string = [oldString stringByReplacingOccurrencesofString: @"\n" withString: @"\\n"];
// repeat for \t  and \r
于 2010-06-30T12:56:13.483 回答