![](/img/trans.png)
[英]Convert from Byte Array to String and compare against another String (java)
[英]Compare a String reconstructed from a byte array with another String
有人可以指出我正確的方向嗎?
我收到一個數據報包,我將其存儲到字符串中。 當我將該字符串與另一個字符串進行比較時,將導致錯誤。
這是代碼:
private static String KEY = "Iamastring";
String sentence = new String( receivePacket.getData(), "UTF-8"); //Reconstruct the string
sentence.trim();
System.out.println("RECEIVED: " + sentence + " test " + KEY + " test ");
System.out.println("Length: " + sentence.length() + " " + KEY.equals(sentence));
這是我的輸出:
RECEIVED: Iamastring
Length: 1024 false
KEY.equals(sentence)的預期比較是正確的。
字符串不可變:p
因此,您必須將sentence.trim()
.trim sentence.trim()
分配給變量,然后將該變量用於比較和sysouts
sentence=sentence.trim();
如果您希望trim()能為您提供幫助,則必須
句子=句子.trim();
因為trim()本身不會修改原始字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.