简体   繁体   English

Ruby - 将所有字符切片直到字符串中的下划线

[英]Ruby - slice all characters till underscore in a string

I have a string like this solution_10 and I would like to remove the part solution_ from it, the number after the underscore will increase, it can be 100 , 1000 and even larger. 我有一个这样的字符串solution_10 ,我想删除的部分solution_从中,下划线将增加后的数字,也可以是1001000 ,甚至更大。 I cant seem to wrap my head around on how to do this. 我似乎无法绕过如何做到这一点。

I have tried to use slice!(0, 9) but that gives me solution_ , I then tried slice!(0, -2) but that gives me null, 我曾尝试使用slice!(0, 9)但是这给了我solution_ _然后我尝试slice!(0, -2)但是这给了我null,

I then tried using solution_10[1..9] this gives me ortable_1 然后我尝试使用solution_10[1..9] ortable_1 solution_10[1..9]这给了我ortable_1

So my question is how to get rid of all characters till underscore , all I want is the number after the underscore. 所以我的问题是如何摆脱所有字符直到underscore ,我想要的只是下划线后面的数字。

Use String#split method 使用String#split方法

'solution_10'.split('_').last #will return original string if no underscore present
#=> "10"

'solution_10'.split('_')[1] #will return nil if no underscore present
#=> "10"
"solution_10"[/(?<=_).*/]
#⇒ "10"

or simply just get digits until the end of the line: 或者只是获取数字直到行尾:

"solution_10"[/\d+\z/]
#⇒ "10"

Another way: 其他方式:

'solution_10'[/\d+/]
#=> "10"

I cant seem to wrap my head around on how to do this. 我似乎无法绕过如何做到这一点。

First of all, slice and its shortcut [] can be used in many ways. 首先, slice及其快捷方式[]可以在很多方面使用。 One way is by providing a start index and a length : 一种方法是提供起始索引和长度

'hello'[2, 3] #=> "llo" # 3 characters, starting at index 2
#  ^^^

You can use that variant if you know the length in advance. 如果您事先知道长度,则可以使用该变体。 But since the number part in your string could be 10 or 100 or 1000 , you don't. 但由于你的字符串中的数字部分可能是101001000 ,所以你没有。

Another way is to provide a range, denoting the start and end index: 另一种方法是提供一个范围,表示开始结束索引:

'hello'[2..3] #=> "ll"  # substring from index 2 to index 3
#  ^^

In this variant, Ruby will determine the length for you. 在这个变体中,Ruby将为您确定长度。 You can also provide negative indices to count from the end. 您还可以提供从最后开始计算的负数指数。 -1 is the last character, -2 the second to last and so on. -1是最后一个字符, -2是倒数第二个字符,依此类推。

So my question is how to get rid of all characters till underscore, all I want is the number after the underscore. 所以我的问题是如何摆脱所有字符直到下划线,我想要的只是下划线后面的数字。

We have to get the index of the underscore: 我们必须得到下划线的index

s = "solution_10"
i = s.index('_') #=> 8

Now we can get the substring from that index to the last character via: 现在我们可以通过以下方式从该索引获取子字符串到最后一个字符:

s[i..-1] #=> "_10"

Apparently, we're off by one, so let's add 1: 显然,我们是一个人,所以让我们加1:

s[i+1..-1] #=> "10"

There you go. 你去吧


Note that this approach will not necessarily return a number (or numeric string), it will simply return everything after the first underscore: 请注意,此方法不一定会返回一个数字(或数字字符串),它只会返回第一个下划线后的所有内容:

s = 'foo_bar'
i = s.index('_') #=> 3
s[i+1..-1]       #=> "bar"

It will also fail miserably if the string does not contain an underscore, because i would be nil : 如果字符串不包含下划线,它也将失败,因为i将是nil

s = 'foo'
i = s.index('_') #=> nil
s[i+1..-1]       #=> NoMethodError: undefined method `+' for nil:NilClass

For a more robust solution, you can pass a regular expression to slice / [] as already shown in the other answers. 对于更强大的解决方案,您可以将正则表达式传递给slice / []如其他答案中所示。 Here's a version that matches an underscored followed by a number at the end of the string. 这是一个匹配下划线后跟字符串末尾的数字的版本。 The number part is captured and returned: 捕获并返回数字部分:

"solution_10"[/_(\d+)\z/, 1] #=> "10"
#              _          literal underscore
#               (   )     capture group (the `1` argument refers to this)
#                \d+      one or more digits
#                    \z   end of string

Why don't just make use of regex 为什么不只使用正则表达式

"solution_10".scan(/\d+/).last
#=> "10"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM