4
考慮下面的輸入採用分體式功能答案:差異而在Ruby中
file1, length1, name1, title1 = line2.split(/,\s*/)
我得到以下輸出:
line1 = "Hey | Hello | Good | Morning"
line2 = "Hey , Hello , Good , Morning"
file1=length1=name1=title1=nil
使用「」以如下分割字符串
puts file1,length1,name1,title1
>Hey
>Hello
>Good
>Morning
但是,使用'|'到分割字符串我接收一個不同的輸出:
file1, length1, name1, title1 = line2.split(/|\s*/)
puts file1,length1,name1,title1
>H
>e
>y
這兩個字符串是除了分離符號(在第一種情況下一個逗號和在第二種情況下的配管)相同。我使用的分割函數的格式也是相同的,當然,除了分隔字符之外。是什麼導致這種變化?
謝謝它回答了我的問題並清除了我的概念。 – randomuser15995183