分解分裂

    科技2026-08-07  1

    目录 (Table of Contents)

    Introduction

    介绍

    Vocabulary Review

    词汇复习

    So… what is the split method exactly?

    那么……什么是split方法呢?

    What can I do with the split method?

    我可以用split方法做什么?

    4a.

    4a。

    Split with NO pattern and/or limit

    以NO模式和/或限制分割

    4b.

    4b。

    Split with ONLY pattern

    仅用模式分割

    4c.

    4c。

    Split with pattern AND limit

    按模式和限制进行拆分

    4d.

    4d。

    Something to put out there…

    东西放在那里...

    Conclusion & Takeaways

    结论与总结

    Want more? Other resources to check out!

    想要更多? 其他资源请查看!

    介绍 (Introduction)

    (Don’t have time? Skip to the next section for the good stuff!)

    (没有时间吗?跳到 下一节 了解好东西!)

    As someone who has a little more than nine weeks of learning and experience with coding, I am very grateful that Ruby is my first programming language. Although I don’t know much about other programming languages to compare, there is so much that I appreciate about Ruby. For example, Ruby has built-in methods that make my life easier…

    作为一个拥有超过九周的学习和编程经验的人,我非常感谢Ruby是我的第一门编程语言。 尽管我对其他编程语言了解不多,但我对Ruby感到非常欣赏。 例如,Ruby具有使我的生活更轻松的内置方法。

    …actually that’s not entirely true. Because Ruby has so many methods to make life easier, it’s hard to remember them all (not to mention how to use them). Thankfully, you can always check the documentation so you don’t have to!

    …实际上并非完全如此。 由于Ruby有许多使生活更轻松的方法,因此很难记住所有这些方法(更不用说如何使用它们了)。 值得庆幸的是,您可以随时检查文档,因此您不必这样做!

    One of the more powerful methods at our disposal is split because of its ability to manipulate strings. To be more specific, the split method allows us to divide a string into an array in multiple ways. This comes in handy as a key tool for us to use when we want to see or change our data in a certain way.

    我们可以使用的更强大的方法之一是拆分,因为它具有处理字符串的能力。 更具体地说,split方法允许我们以多种方式将字符串分成数组。 当我们希望以某种方式查看或更改数据时,这是方便我们使用的关键工具。

    Coincidentally, this was also the one method that I kept needing to refer to the ruby documentation for. Since the split method is so versatile, I sometimes get confused on how to use it to get what I want. If you’re like me and need a clear breakdown on the split method, what it can do, and how you can get it to do what you want, look no further!

    碰巧的是,这也是我一直需要参考ruby文档的一种方法。 由于split方法用途广泛,因此有时我对如何使用它获取所需内容感到困惑。 如果您像我一样,并且需要对split方法,它可以做什么以及如何使它执行所需的操作进行清晰的分类,请不要再犹豫!

    词汇复习👀 (Vocabulary Review 👀)

    Before we get started, here’s a quick review of important vocabulary to know:

    在我们开始之前,这里是对重要词汇的快速回顾,要了解:

    Strings in Ruby: a data type that represents textual data; a sequence of characters enclosed in double or single quotes.

    Ruby中的字符串 :表示文本数据的数据类型; 用双引号或单引号引起来的字符序列。

    Arrays in Ruby: a data type that is a collection of data separated by commas; can store any data type.

    Ruby中的数组 :一种数据类型,它是用逗号分隔的数据的集合; 可以存储任何数据类型。

    Delimiters: one or more characters that separate text strings (from Computer Hope).

    分隔符 :一个或多个字符,用于分隔文本字符串(来自Computer Hope )。

    Ruby Regular Expressions (regex): helps us to find particular patterns inside a string; they are declared between two forward slashes “/ /” and its equivalent regex literal is %r{…} (from GeeksforGeeks)

    Ruby正则表达式(regex) :帮助我们在字符串中查找特定模式; 它们在两个正斜杠“ / /”之间声明,其等效的正则表达式文字为%r {…}(来自GeeksforGeeks )

    Whitespace: any character or series of characters that represent horizontal or vertical space in typography (from Wikipedia)

    空格 :表示排版中水平或垂直空间的任何字符或一系列字符(来自Wikipedia )

    Parameters: local variables to be used within the method; they are placeholders, not the actual values to be used (from Learn.co)

    参数 :方法中要使用的局部变量; 它们是占位符,而不是要使用的实际值(来自Learn.co )

    Alright, now that we have all the information we need to understand split, let’s learn more about the syntax and how to use it!

    好了,既然我们已经掌握了理解split所需的所有信息,那么让我们进一步了解语法以及如何使用它吧!

    那么……究竟是什么分割方法? 🤔 (So…what is the split method exactly? 🤔)

    split is a string class method that returns a new array with substrings based on the specified pattern or delimiter.

    split是一个字符串类方法,该方法根据指定的模式或分隔符返回带有子字符串的新数组。

    The syntax to use the split method is:

    使用split方法的语法为:

    array = string.split(pattern, limit) “pattern” can be a string or regex (regular expression)

    “模式”可以是字符串或正则表达式(正则表达式)

    “limit” is an optional second parameter to specify the number of substrings/entries into the returned array.

    “ limit”是可选的第二个参数,用于指定返回的数组中的子字符串/条目的数量。

    🧐 Important Note: Since limit is the optional second parameter, it is not necessary to include unless you want to limit your substrings; you will never pass a limit without pattern.

    🧐重要说明 :由于limit是可选的第二个参数,除非您想限制子字符串,否则不必包括它; 您将永远不会超越没有模式的限制。

    我可以用split方法做什么? ✍🏼 (What can I do with the split method? ✍🏼)

    In order to understand how to use the split method, I will be using the examples from the ruby documentation and interpreting into my “plain English”. Please note that I’ve interpreted what the ruby documentation refers to “null fields” as whitespaces.

    为了理解如何使用split方法,我将使用ruby文档中的示例并将其解释为“普通英语”。 请注意,我已经将ruby文档所指的“空字段”解释为空格。

    以NO模式和/或限制分割: (Split with NO pattern and/or limit:)

    As mentioned above, if you do not split with pattern, you will not be splitting with limit either. However, you can split with only pattern; it’s important to know how your returned array will be affected if there is no limit.

    如上所述,如果不使用模式拆分,则也不会使用限制拆分。 但是,您只能使用模式进行拆分; 重要的是要知道,如果没有限制,返回的数组将受到怎样的影响。

    " now's the time ".split #=> ["now's", "the", "time"] If no pattern is given (pattern = nil), the default is to split the string based on whitespace.

    如果未提供任何模式(pattern = nil),则默认值为根据空白分割字符串。 If no limit is passed in, if there is any trailing whitespace it will not be included in the returned array.

    如果没有传递任何限制,则如果有任何尾随空格,它将不包含在返回的数组中。

    Breakdown of the example: The example above doesn’t have pattern or limit. Because there is no pattern, the string is divided on the whitespaces; because there is no limit the (trailing) whitespaces after “time” is not included.

    示例的分解 :上面的示例没有模式或限制。 因为没有模式,所以字符串在空格上分开; 因为没有限制,所以不包括“时间”之后的(尾随)空格。

    仅用模式分割: (Split with ONLY pattern:)

    In general, wherever the content of the pattern’s string matches the string it is called on, that’s where it will divide the substring.

    通常,只要模式字符串的内容与调用它的字符串相匹配,就会在该位置划分子字符串。

    " now's the time".split(' ') #=> ["now's", "the", "time"]"mellow yellow".split("ello") #=> ["m", "w y", "w"]"1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"]

    If pattern is a string, the content of the pattern’s string will be used as the delimiter.

    如果pattern是字符串 ,则模式字符串的内容将用作定界符。

    If the pattern’s string is a whitespace, the string called on will be split on the whitespaces with leading, trailing, and runs of contiguous whitespace characters ignored.

    如果模式的字符串是空格,则调用的字符串将在空格上分割,忽略前导,尾随和连续的连续空格字符。 " now's the time".split(/ /) #=> ["", "now's", "", "the", "time"]"hello".split(//) #=> ["h", "e", "l", "l", "o"]"1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]"hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]

    If pattern is a regex, the string it is called on will be divided where the pattern matches.

    如果pattern是一个regex ,则将在匹配模式的地方对调用它的字符串进行分割。

    If pattern’s regex is a zero-length string, the string called on will be divided into individual characters.

    如果pattern的正则表达式是长度为零的字符串,则称为on的字符串将被分为单个字符。

    Note: A huge benefit to using regex is that it can split by multiple delimiters (capture groups). While the capture groups will not count towards the limit, they will be included in the resulting array. To read more, check out here and here.

    注意 :使用正则表达式的一个巨大好处是它可以被多个定界符(捕获组)分割。 尽管捕获组不会计入限制,但它们将包含在结果数组中。 要了解更多信息,请在此处和此处查看 。

    用模式AND限制分割: (Split with pattern AND limit:)

    🤓 Check-in time! Why is this “Split with pattern AND limit” vs. “Split with ONLY limit”?

    🤓入住时间! 为什么“用模式AND限制拆分”与“仅用限制拆分”?

    Since you will be passing in an integer for the limit, it’s important to know that the value of the integer will give you different results in your returned array.

    由于您将传入整数作为限制,因此重要的是要知道整数值将在返回的数组中为您提供不同的结果。

    "hello".split(//, 3) #=> ["h", "e", "llo"]"1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]

    If a positive number for the limit is passed in, the maximum number of substrings will be returned.

    如果传入限制的正数 ,则将返回最大子字符串数。

    As seen in the above examples, if your limit is less than the number of where you want to divide by, the last substring may not be divided where you specified in the pattern.

    从上面的示例中可以看到,如果您的限制小于要除以的位置数,则最后一个子字符串可能不会在模式中指定的位置除以。

    Breakdown of the first example: the pattern’s regex is a zero-length string so it is asking for the string to be divided into individual characters. The limit is saying that it wants the array to return three substrings. Since there are five individual characters but the limit only wants three, the result is that the first two substrings follow the pattern and the last (third) substring does not divide.

    第一个示例的细目 :模式的正则表达式是长度为零的字符串,因此它要求将字符串分成单个字符。 限制是说它希望数组返回三个子字符串。 由于有五个单独的字符,但限制只需要三个,因此结果是前两个子字符串遵循该模式,而最后一个(第三个)子字符串不分割。

    "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""]

    If a negative number for the limit is passed in, there is no limit to the number of substrings returned.

    如果传入的限制数为负数 ,则返回的子字符串数没有限制。

    Note that the returned array can include substrings of trailing whitespaces (null fields).

    请注意,返回的数组可以包含尾随空白的子字符串(空字段)。 "mellow yellow".split("ello", 1) #=> ["mellow yellow"]

    If the limit is equal to one, the entire original string will be returned in the array.

    如果限制等于1,则整个原始字符串将在数组中返回。

    This is because the string you are calling the split method on is already one string, and thus will be the only entry; if you have one apple, you can’t ask it to split into one apple 🍎

    这是因为您正在调用split方法的字符串已经是一个字符串,因此将是唯一的条目。 如果您有一个苹果,就不能要求它分裂成一个苹果🍎

    东西放在那里... (Something to put out there…)

    This might be obvious, but not something we would necessarily think about right away 🙈🙉🙊

    这可能是显而易见的,但不是我们必须马上考虑的事情🙈🙉🙊

    "".split(',', -1) #=> []

    If the string you are calling the split method on is empty, the returned array will also be empty! Even if you provide the pattern and limit, the input string will have nothing to divide, thus returning an empty array.

    如果您正在调用split方法的字符串为空 ,则返回的数组也将为空! 即使您提供了模式和限制,输入字符串也不会分割,因此将返回一个空数组。

    结论与要点📝 (Conclusion & Takeaways 📝)

    To put it simply, there is so much you can do with Ruby’s split method to manipulate strings. As long as you know what you want your returned array to look like you will know how to use the split method 😉

    简单地说,有这么多你可以用Ruby的拆分方法操作字符串做。 只要您知道希望返回的数组看起来像什么,就将知道如何使用split方法method

    Things to think about 💬

    关于💬的思考

    Where do you want to divide the substrings?

    您想在哪里划分子字符串? Do you want to divide the string by multiple delimiters?

    是否要用多个分隔符分隔字符串? Do you want whitespaces? If so, which whitespaces would you want?

    你要空格吗? 如果是这样,您想要哪些空格?

    Important things to save 💾

    重要的事情save

    The syntax for the split method is: array = string.split(pattern, limit)

    split方法的语法为:array = string.split(pattern,limit)

    Depending on if pattern is a string or a regex, the syntax inside the pattern will cause different results for your array.

    根据pattern是字符串还是regex ,pattern中的语法会导致数组的结果不同。

    Depending on the value of the integer passed in for limit, the resulting array will be affected.

    根据不同的值在为极限传递的整数,所产生的阵列将受到影响。

    You can split without pattern and/or limit, however you cannot split with only limit (ding ding ding! This is also the answer to our check-in time 🥳)

    您可以不带图案和/或限制进行拆分,但是不能仅带限制进行拆分(ding ding ding!这也是我们入住时间 🥳的答案)

    想要更多? 其他资源请查看! (Want more? Other resources to check out!)

    Ruby Datatypes: 📖 https://learn.co/lessons/data-types-readme📖 https://www.geeksforgeeks.org/ruby-data-types/

    Ruby的数据类型:📖 https://learn.co/lessons/data-types-readme 📖 https://www.geeksforgeeks.org/ruby-data-types/

    Ruby regex: 📖 https://stackoverflow.com/questions/37199667/the-meaning-of-splitr-s

    Ruby的正则表达式:📖 https://stackoverflow.com/questions/37199667/the-meaning-of-splitr-s

    Ruby Parameters: 📖 https://medium.com/podiihq/ruby-parameters-c178fdcd1f4e

    Ruby参数:📖 https://medium.com/podiihq/ruby-parameters-c178fdcd1f4e

    If pattern is regex: 📖 https://stackoverflow.com/questions/19509307/split-string-by-multiple-delimiters

    如果模式是正则表达式:📖 https://stackoverflow.com/questions/19509307/split-string-by-multiple-delimiters

    More about the split method:📖 https://www.geeksforgeeks.org/ruby-string-split-method-with-examples/📖 https://code-maven.com/ruby-split📖 https://apidock.com/ruby/String/split📖 https://spin.atomicobject.com/2007/11/01/ruby-string-split/

    有关拆分方法的更多信息:📖https : //www.geeksforgeeks.org/ruby-string-split-method-with-examples/📖https : //code-maven.com/ruby-split📖https :// apidock。 com / ruby​​ / String / split📖https : //spin.atomicobject.com/2007/11/01/ruby-string-split/

    翻译自: https://medium.com/swlh/breaking-down-the-split-3648b7b1c0

    相关资源:对称分裂
    Processed: 0.009, SQL: 9