Kotlin`split`在脚本和编译模式下给出不同的结果

时间:2018-02-13 10:41:50

标签: java split kotlin

我有以下字符串(基本上是原始HTTP请求):

GET / HTTP/1.1            
User-Agent: Wget/1.19.1 (linux-gnu)                  
Accept: */*               
Accept-Encoding: identity 
Host: 0.0.0.0:5000        
Connection: Keep-Alive    

它存储在this.requestData中。我的功能类似于:

println(this.requestData)                                                                                                                                                                         
var lines = this.requestData.split("\n")                                                                                                                                                          
println(lines)

这个输出我正在编译并运行程序:

GET / HTTP/1.1           
User-Agent: Wget/1.19.1 (linux-gnu)                  
Accept: */*               
Accept-Encoding: identity 
Host: 0.0.0.0:5000        
Connection: Keep-Alive    


, ]onnection: Keep-Aliveitylinux-gnu)

这意味着split电话肯定有​​问题。但是,如果我在交互式shell中测试我的代码:

$ kotlinc                                                                       
Welcome to Kotlin version 1.2.21 (JRE 1.8.0_151-8u151-b12-0ubuntu0.17.10.2-b12)                           
Type :help for help, :quit for quit                  
>>> var requestData = """GET / HTTP/1.1                                                                   
... User-Agent: Wget/1.19.1 (linux-gnu)                                                                   
... Accept: */*                                      
... Accept-Encoding: identity                        
... Host: 0.0.0.0:5000                               
... Connection: Keep-Alive                           
...                       
...                       
... """                   
>>> var lines = requestData.split("\n")
>>> lines
[GET / HTTP/1.1           , User-Agent: Wget/1.19.1 (linux-gnu)                  , Accept: */*               , Accept-Encoding: identity , Host: 0.0.0.0:5000        , Connection: Keep-Alive    , , , ]

我得到了预期的结果。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

正如@haraldK在评论中指出的那样,我通过在"\r\n"

分裂来使其正常工作