# c#string分割字符串_使用String＃split方法在Ruby中分割字符串

c#string分割字符串

Unless user input is a single word or number, that input will need to be split or turned into a list of strings or numbers.

For instance, if a program asks for your full name, including middle initial, it will first need to split that input into three separate strings before it can work with your individual first, middle and last name. This is achieved using the String#split method.

## String＃split如何工作 ( How String#split Works )

In its most basic form, String#split takes a single argument: the field delimiter as a string. This delimiter will be removed from the output and an array of strings split on the delimiter will be returned.

So, in the following example, assuming the user input their name correctly, you should receive a three-element Array from the split.


#!/usr/bin/env rubyprint "What is your full name? "full_name = gets.chompname = full_name.split(' ')puts "Your first name is #{name.first}"puts "Your last name is #{name.last}"


If we run this program and enter a name, we'll get some expected results. Also, note that name.first and name.last are coincidences. The name variable will be an Array, and those two method calls will be equivalent to name[0] and name[-1] respectively.


$ruby split.rbWhat is your full name? Michael C. MorinYour first name is MichaelYour last name is Morin  However, String#split is a bit smarter than you'd think. If the argument to String#split is a string, it does indeed use that as the delimiter, but if the argument is a string with a single space (as we used), then it infers that you want to split on any amount of whitespace and that you also want to remove any leading whitespace. 但是， String＃split比您想象的要聪明。 如果String＃split的参数是一个字符串，它的确将其用作分隔符，但是如果该参数是一个带有单个空格的字符串(如我们所用)，则它将推断您希望在任意数量的空格上进行拆分并且您还想删除任何前导空格。 So, if we were to give it some slightly malformed input such as 因此，如果我们要给它一些格式错误的输入，例如  Michael C. Morin  (with extra spaces), then String#split would still do what is expected. However, that's the only special case when you pass a String as the first argument. Regular Expression Delimiters (带有多余的空格)，那么String＃split仍会执行预期的操作。 但是，这是当您将String作为第一个参数传递时的唯一特殊情况。 正则表达式分隔符 You can also pass a regular expression as the first argument. Here, String#split becomes a bit more flexible. We can also make our little name splitting code a bit smarter. 您还可以将正则表达式作为第一个参数传递。 在这里， String＃split变得更加灵活。 我们还可以使我们的小名拆分代码更加智能。 We don't want the period at the end of the middle initial. We know it's a middle initial, and the database won't want a period there, so we can remove it while we split. When String#split matches a regular expression, it does the same exact thing as if it had just matched a string delimiter: it takes it out of the output and splits it at that point. 我们不希望中间首字母的结尾处有句点。 我们知道这是一个中间名首字母，并且数据库在此不需要句号，因此我们可以在拆分时将其删除。 当String＃split匹配正则表达式时，它会执行与刚匹配字符串定界符相同的操作：将其从输出中取出并在那一点进行拆分。 So, we can evolve our example a little bit: 因此，我们可以对示例进行一些改进： $ cat split.rb#!/usr/bin/env rubyprint "What is your full name? "full_name = gets.chompname = full_name.split(/\.?\s+/)puts "Your first name is #{name.first}"puts "Your middle initial is #{name[1]}"puts "Your last name is #{name.last}"


## 默认记录分隔符 ( Default Record Separator )

Ruby is not really big on "special variables" that you might find in languages like Perl, but String#split does use one you need to be aware of. This is the default record separator variable, also known as $;. Ruby在诸如Perl之类的语言中对“特殊变量”的重视并不大，但是String＃split确实使用了您需要注意的一种。 这是默认的记录分隔符变量，也称为$；

It's a global, something you don't often see in Ruby, so if you change it, it might affect other parts of the code—just be sure to change it back when finished.

However, all this variable does is act as the default value for the first argument to String#split. By default, this variable seems to be set to nil. However, if String#split's first argument is nil, it will replace it with a single space string.

## 零长度分隔符 ( Zero-Length Delimiters )

If the delimiter passed to String#split is a zero-length string or regular expression, then String#split will act a bit differently. It will remove nothing at all from the original string and split on every character. This essentially turns the string into an array of equal length containing only one-character strings, one for each character in the string.

This can be useful for iterating over the string and was used in pre-1.9.x and pre-1.8.7 (which backported a number of features from 1.9.x) to iterate over characters in a string without worrying about breaking up multi-byte Unicode characters. However, if what you really want to do is iterate over a string, and you're using 1.8.7 or 1.9.x, you should probably use String#each_char instead.


#!/usr/bin/env rubystr = "She turned me into a newt!"str.split('').each do|c| puts cend


## 限制返回数组的长度 ( Limiting The Length of the Returned Array )

So back to our name parsing example, what if someone has a space in their last name? For instance, Dutch surnames can often begin with "van" (meaning "of" or "from").

We only really want a 3-element array, so we can use the second argument to String#split that we have so far ignored. The second argument is expected to be a Fixnum. If this argument is positive, at most, that many elements will be filled in the array. So in our case, we would want to pass 3 for this argument.


#!/usr/bin/env rubyprint "What is your full name? "full_name = gets.chompname = full_name.split(/\.?\s+/, 3)puts "Your first name is #{name.first}"puts "Your middle initial is #{name[1]}"puts "Your last name is #{name.last}"


If we run this again and give it a Dutch name, it will act as expected.


\$ ruby split.rbWhat is your full name? Vincent Willem van GoghYour first name is VincentYour middle initial is WillemYour last name is van Gogh


However, if this argument is negative (any negative number), then there will be no limit on the number of elements in the output array and any trailing delimiters will appear as zero-length strings at the end of the array.

This is demonstrated in this IRB snippet:


:001 > "this,is,a,test,,,,".split(',', -1) => ["this", "is", "a", "test", "", "", "", ""] 


c#string分割字符串

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

07-02
05-29 3万+

11-30 923
09-08 100
05-11 188
12-04 3282
02-17 153
05-07 1万+
03-02 4789
11-04 9274