开发者

Android removing newline characters

TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter('\n');
    splitter.setString(tweetMessage);
    tweetMessage = "";
    for (String s : splitter) 
    {
        Log.v(getClass().getSimpleName(), "Spliter :: "+ s);
        tweetMessage +=s;
    }

But this shows up the newline char开发者_StackOverflow社区acters as Squares how do I get Rid of that


The characters you're seeing as squares probably aren't '\n' ('\u000A') characters.

If you're seeing a multiple lines in your output then it's possible that multiple characters are being used to split the lines. It might be '\r\n' but it could be something else.

Your best bet is to use a debugger to check exactly what the String contains before you split it. Or you could add some debug code like so:

for (int u : tweetMessage.toCharArray()) {
    Log.v(getClass().getSimpleName(), String.format("\\u%04X",u));
}

If you see \u000D \u000A then your lines are separated by "\r\n". This means you can't use a SimpleStringSplitter to split the string since that will only split on a single character.

If you don't want to parse the string manually, you could use the String.split() method:

for (String s : tweetMessage.split("\r\n") {
    //do something with s
}


If you need to remove newline/tab characters at the beginning and the end of a string (not the ones in the middle of the string) just use .trim() function to do the tric. This will remove white spaces from the begging and the end of the string.

Ex:

String xmlVersionDate = rootNode.getChildNodeWithName("last-updated").getValue().trim();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜