Java Clean String?

时间:2015-05-25 20:59:35

标签: java string sockets tcp

所以我正在使用TCP,我从DataInputStream获取数据,但是当我得到数据时,它有像^ X ^ @ ^ E ^ R这样的东西。什么时候不应该这样做。无论如何我可以删除它。但它需要是动态的。

DataInputStream dataIn = new DataInputStream(socket.getInputStream());

StringBuffer inputLine = new StringBuffer();
String tmp;

while((tmp = dataIn.readLine()) != null){
    //tmp = Normalizer.normalize(tmp, Normalizer.Form.NFD);

    inputLine.append(tmp);
    logText(tmp);
}

2 个答案:

答案 0 :(得分:1)

DataInputStream.readLine()已弃用。请改用BufferedReader。

例如: BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

我如何读行:

while ((socket != null) && !(socket.isClosed())) {
    // read in a line but only if there is one - blocks here till there is a line

    String clientSentence = input.readLine();

    if ((clientSentence != null) && (!(clientSentence.equals("")))) {
         // do something
    }
}

答案 1 :(得分:0)

要删除所有控制字符(小数32以下的字节),您可以执行以下操作:

tmp = tmp.replaceAll("[\000-\01F]", "");