当它应该返回一个正数时,java indexOf返回-1

Arm*_*min 3 java network-programming substring indexof datainputstream

我是网络编程的新手,之前从未使用过Java进行网络编程.我正在使用Java编写服务器,我从客户端处理消息时遇到了一些问题.我用了

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 ));      
}
Run Code Online (Sandbox Code Playgroud)

如果我这样做,并有一个示例输入asg 23\aag,它将返回:

Found Index :-1
Found Index :3
Found Index :9
Run Code Online (Sandbox Code Playgroud)

很明显,如果String对象是从头开始创建的,indexOf可以找到"\".如果从处理DataInputStream获得String,代码如何定位问题?

Joh*_*136 9

尝试String abc=new String("\\a");- 您需要\\在字符串中获得反斜杠,否则\定义"转义序列"的开头.