Ruby - 如何从字符串中选择一些字符

发布于 2024-11-16 16:28:38 字数 174 浏览 5 评论 0原文

我试图找到一个函数来选择例如字符串的前 100 个字符。在 PHP 中,存在 substr 函数

Ruby有类似的功能吗?

I am trying to find a function for select e.g. first 100 chars of the string. In PHP, there exists the substr function

Does Ruby have some similar function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清君侧 2024-11-23 16:28:38

尝试 foo[0...100],任何范围都可以。范围也可以为负值。 Ruby 文档对此进行了很好的解释

Try foo[0...100], any range will do. Ranges can also go negative. It is well explained in the documentation of Ruby.

冷了相思 2024-11-23 16:28:38

使用 [] 运算符 (docs):

foo[0, 100]  # Get 100 characters starting at position 0
foo[0..99]   # Get all characters in index range 0 to 99 (inclusive!)
foo[0...100] # Get all characters in index range 0 to 100 (exclusive!)

更新为Ruby 2.7无起始范围 现在就在这里(截至 2019 年 12 月 25 日),可能是“返回数组的第一个 xx”的规范答案:

foo[...100]  # Get all chars from the beginning up until the 100th (exclusive)

使用 .slice方法(docs):

foo.slice(0, 100)  # Get 100 characters starting at position 0
foo.slice(0...100) # Behaves the same as operator [] 

为了完整性:

foo[0]         # Returns the indexed character, the first in this case
foo[-100, 100] # Get 100 characters starting at position -100
               # Negative indices are counted from the end of the string/array
               # Caution: Negative indices are 1-based, the last element is -1
foo[-100..-1]  # Get the last 100 characters in order
foo[-1..-100]  # Get the last 100 characters in reverse order
foo[-100...foo.length] # No index for one beyond last character

Ruby 2.6 更新 : 无尽的范围现在就在这里(如2018-12-25)!

foo[0..]      # Get all chars starting at the first. Identical to foo[0..-1]
foo[-100..]   # Get the last 100 characters

Using the []-operator (docs):

foo[0, 100]  # Get 100 characters starting at position 0
foo[0..99]   # Get all characters in index range 0 to 99 (inclusive!)
foo[0...100] # Get all characters in index range 0 to 100 (exclusive!)

Update for Ruby 2.7: Beginless ranges are here now (as of 2019-12-25) and are probably the canonical answer for "return the first xx of an array":

foo[...100]  # Get all chars from the beginning up until the 100th (exclusive)

Using .slice method (docs):

foo.slice(0, 100)  # Get 100 characters starting at position 0
foo.slice(0...100) # Behaves the same as operator [] 

And for completeness:

foo[0]         # Returns the indexed character, the first in this case
foo[-100, 100] # Get 100 characters starting at position -100
               # Negative indices are counted from the end of the string/array
               # Caution: Negative indices are 1-based, the last element is -1
foo[-100..-1]  # Get the last 100 characters in order
foo[-1..-100]  # Get the last 100 characters in reverse order
foo[-100...foo.length] # No index for one beyond last character

Update for Ruby 2.6: Endless ranges are here now (as of 2018-12-25)!

foo[0..]      # Get all chars starting at the first. Identical to foo[0..-1]
foo[-100..]   # Get the last 100 characters
┈┾☆殇 2024-11-23 16:28:38

foo.first(100) 其中 foo 是一个字符串

foo.first(100) where foo is a String

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文