如果弦比90弦长,如何将它分成两部分

时间:2021-04-27 21:41:15

I have a dynamic string (in Ruby program) and I need to split it if it is longer then 91 chars. I need to split it into parts on closes comma ',' char.

我有一个动态字符串(在Ruby程序中),如果它比91个字符长,我需要将它拆分。我需要把它分割成闭逗号,char。

String example:

字符串的例子:

"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41"

Thank you!

谢谢你!

3 个解决方案

#1


6  

This is what you need:

这就是你所需要的:

string.split(',').in_groups_of(90, false)

Result:

结果:

> str.split(',').in_groups_of(10, false)
=> [["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
 ["11", "12", "13", "14", "15", "16", "17", "18", "19", "20"],
 ["21", "22", "23", "24", "25", "26", "27", "28", "29", "30"],
 ["31", "32", "33", "34", "35", "36", "37", "38", "39", "40"],
 ["41"]]

Or an array with joined values:

或具有连接值的数组:

str.split(',').in_groups_of(10, false).map {|s| s.join(',')}

Result:

结果:

> str.split(',').in_groups_of(10, false).map {|s| s.join(',')}
=> ["1,2,3,4,5,6,7,8,9,10",
 "11,12,13,14,15,16,17,18,19,20",
 "21,22,23,24,25,26,27,28,29,30",
 "31,32,33,34,35,36,37,38,39,40",
 "41"]

UPDATE:

更新:

With plain Ruby (not Rails):

使用纯Ruby(不是Rails):

str.split(',').each_slice(10).to_a

or joined:

或加入:

str.split(',').each_slice(10).map {|s| s.join(',')}

#2


1  

try this

试试这个

  str.split(',') if str.length>91

#3


0  

I'm assuming you need something that takes a String and returns an Array where each element of the Array is a string with length 91 or less. But you want the resulting strings to be as long as possible before splitting on the delimiter.

我假设你需要一个字符串并返回一个数组其中数组的每个元素都是长度为91或更少的字符串。但是,在对分隔符进行拆分之前,您希望得到的字符串尽可能长。

a = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41"

def my_split(str, delimiter, max_length)
    return [str] if str.length <= max_length
    i = str.rindex(delimiter, max_length)
    the_rest = my_split(str[i+1..-1], delimiter, max_length)
    return [str[0..i-1]] + the_rest
end

for r in my_split(a, ',', 90)
    puts "#{r}:Length: #{r.length.to_s}"
end

results with:

结果:

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33:Length: 89
34,35,36,37,38,39,40,41:Length: 23

#1


6  

This is what you need:

这就是你所需要的:

string.split(',').in_groups_of(90, false)

Result:

结果:

> str.split(',').in_groups_of(10, false)
=> [["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
 ["11", "12", "13", "14", "15", "16", "17", "18", "19", "20"],
 ["21", "22", "23", "24", "25", "26", "27", "28", "29", "30"],
 ["31", "32", "33", "34", "35", "36", "37", "38", "39", "40"],
 ["41"]]

Or an array with joined values:

或具有连接值的数组:

str.split(',').in_groups_of(10, false).map {|s| s.join(',')}

Result:

结果:

> str.split(',').in_groups_of(10, false).map {|s| s.join(',')}
=> ["1,2,3,4,5,6,7,8,9,10",
 "11,12,13,14,15,16,17,18,19,20",
 "21,22,23,24,25,26,27,28,29,30",
 "31,32,33,34,35,36,37,38,39,40",
 "41"]

UPDATE:

更新:

With plain Ruby (not Rails):

使用纯Ruby(不是Rails):

str.split(',').each_slice(10).to_a

or joined:

或加入:

str.split(',').each_slice(10).map {|s| s.join(',')}

#2


1  

try this

试试这个

  str.split(',') if str.length>91

#3


0  

I'm assuming you need something that takes a String and returns an Array where each element of the Array is a string with length 91 or less. But you want the resulting strings to be as long as possible before splitting on the delimiter.

我假设你需要一个字符串并返回一个数组其中数组的每个元素都是长度为91或更少的字符串。但是,在对分隔符进行拆分之前,您希望得到的字符串尽可能长。

a = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41"

def my_split(str, delimiter, max_length)
    return [str] if str.length <= max_length
    i = str.rindex(delimiter, max_length)
    the_rest = my_split(str[i+1..-1], delimiter, max_length)
    return [str[0..i-1]] + the_rest
end

for r in my_split(a, ',', 90)
    puts "#{r}:Length: #{r.length.to_s}"
end

results with:

结果:

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33:Length: 89
34,35,36,37,38,39,40,41:Length: 23