我是網絡編程的新手,之前從未使用過Java進行網絡編程。 我正在寫一個使用Java的服務器,並且我有一些來自客戶端的問題處理消息。我用java indexOf當它應該返回一個正數時返回-1
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
while (true) {
// Receive radius from the client
byte[] r=new byte[256000];
inputFromClient.read(r);
String Ffss =new String(r);
System.out.println("Received from client: " + Ffss);
System.out.print("Found Index :");
System.out.println(Ffss.indexOf('\a'));
System.out.print("Found Index :");
System.out.println(Ffss.indexOf(' '));
String Str = new String("add 12341\n13243423");
String SubStr1 = new String("\n");
System.out.print("Found Index :");
System.out.println(Str.indexOf(SubStr1));
}
如果我這樣做,有一個樣品輸入ASG 23 \ AAG,它將返回:
Found Index :-1
Found Index :3
Found Index :9
很明顯的是,如果String對象是從頭開始創建,的indexOf可以找到「\」。 如果從處理DataInputStream獲取字符串,代碼會如何定位\ a?
無關,但不叫字符串對簡單的字符串創建構造函數。例如更喜歡'字符串foo =「foo」'在'字符串foo =新字符串(「foo」)' –
@TheLostMind我沒有從我的編輯器複製代碼,這是一個錯字 – Armin
@SteveKuo我犯了這個錯字,當我粘貼我的代碼後。對不起混淆 – Armin