现在有这样一个问题,我在本地开启一个服务端,然后通过Telnet 127.0.0.1 888888  访问该服务端,提交数据:0\n1Where did the symbol\n10\n1originate?\n0\n1India\n0\n1China\n0\n1Liverpool\n0\na\n
后台得到数据后通过split("\\n")进行分割,但一直无法分割成功。这是什么呢?
在线等....

解决方案 »

  1.   

            String input = "0\n1Where did the symbol\n10\n1originate?\n0\n1India\n0\n1China\n0\n1Liverpool\n0\na\n";
            System.out.println(Arrays.toString(input.split("\n")));
            System.out.println(Arrays.toString(input.split("\\n")));[0, 1Where did the symbol, 10, 1originate?, 0, 1India, 0, 1China, 0, 1Liverpool, 0, a]
    [0, 1Where did the symbol, 10, 1originate?, 0, 1India, 0, 1China, 0, 1Liverpool, 0, a]
      

  2.   

    BufferedReader in =new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
    String s = in.readLine();
    s.split("\\n");telnet 传的字符串是:0\n1Where did the symbol\n10\n1originate?\n0\n1India\n0\n1China\n0\n1Liverpool\n0\na\n
      

  3.   

    呵呵String s = in.readLine();按行读取的,而"\n"就是换行符号。。
      

  4.   

    不明白。。描述一下。。in.readLine()是接收回车的换行啊?跟字符串有什么关系?
      

  5.   

    readLine()就是读取一行String s = in.readLine();把这个s打印出来看看
      

  6.   

    readLine 读到换行就停了。for(String s = in.readLine(); s!= null; s = in.readLine()){
        // process s
    }
      

  7.   

    可参考JDK文档:public String readLine() throws IOException
    读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。 
      

  8.   

    String line = in.readLine();
    while (!line.equals("end")) {
    String msg = createMessage(line);
    addQuestion(line);
    out.println(msg);
    line = in.readLine();
    }
    这是我那块代码块,没问题吧?
      

  9.   

    try {
    String line = in.readLine();
    while (!line.equals("end")) {
    String msg = createMessage(line);
     System.out.println(Arrays.toString(msg.split("\n")));
            System.out.println(Arrays.toString(msg.split("\\n")));
    line = in.readLine();
    }
    out.println("--- See you, bye! ---");
    client.close();
    } catch (IOException e) {
    }
    出来的结果是:
    [0\n1Where did the symbol\n10\n1originate?\n0\n1India\n0\n1China\n0\n1Liverpool\n0\na\n]
    [0\n1Where did the symbol\n10\n1originate?\n0\n1India\n0\n1China\n0\n1Liverpool\n0\na\n]