2013-03-08 78 views
-1

我正在关注一本书的教程,并且有一小段代码我不明白。这里谈到的代码:C#字符串拆分方法

// conv_ex.cs - Exercise 12.4 
// This listing will cut the front of the string 
// off when it finds a space, comma, or period. 
// Letters and other characters will still cause 
// bad data. 
//----------------------------------------------- 
using System; 
using System.Text; 

class myApp 
{ 
    public static void Main() 
    { 
     string buff; 
     int age; 
     // The following sets up an array of characters used 
     // to find a break for the Split method. 
     char[] delim = new char[] { ' ', ',', '.' }; 
     // The following is an array of strings that will 
     // be used to hold the split strings returned from 
     // the split method. 
     string[] nbuff = new string[4]; 

     Console.Write("Enter your age: "); 

     buff = Console.ReadLine(); 

     // break string up if it is in multiple pieces. 
     // Exception handling not added 

     nbuff = buff.Split(delim,2); //<----- what is purpose of (delim,2) in this case? 
     //nbuff = buff.Split(delim); // will give the same result 
     //nbuff = buff.Split(delim,3); //will give the same result 


     // Now convert.... 

     try 
     { 
      age = Convert.ToInt32(nbuff[0]); 
      Console.WriteLine("age is:" + age); 

      if (age < 21) 
       Console.WriteLine("You are under 21."); 
      else 
       Console.Write("You are 21 or older."); 
     } 
     catch (ArgumentException e) 
     { 
      Console.WriteLine("No value was entered... (equal to null)"); 
     } 
     catch (OverflowException e) 
     { 
      Console.WriteLine("You entered a number that is too big or too small."); 
     } 
     catch (FormatException e) 
     { 
      Console.WriteLine("You didn't enter a valid number."); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Something went wrong with the conversion."); 
      throw (e); 
     } 
    } 
} 

我的问题是:

什么是 “” 在nbuff = buff.Split(delim,2);的目的是什么?

无论如何,弦乐会分成两半,对吗?

即使没有“”如在nbuff = buff.Split(delim);结果将是相同的。

+2

http://msdn.microsoft.com/en-us/库/ c1bs0eda。aspx – 2013-03-08 17:03:23

回答

2

这是返回的最大子字符串数。将其更改为3的原因不起作用,因为返回的子字符串少于3个,因此,按照设计,它将返回所有可用的子字符串。 如果有潜在的5子字符串可以返回,例如,那么只有第一个3将被返回。

你可以阅读更多关于String.Split()方法here

+0

好的,但即使它是“1”,或者即使省略了结果也是一样的,那么为什么“2”呢? – 2013-03-08 17:10:47

+0

从上下文中看来,似乎没有任何明确的理由添加了'2',但由于它来自教科书,因此有可能在其他地方解释原因(可能为了准备下一章/代码段或东西)。 – keyboardP 2013-03-08 17:13:09

+0

@nenad您是否使用输入名称时指定的分隔符之一?如果名称不包含指定的分隔符之一,则该字符串不会被分割,并且它将与原始字符串保持一致。换句话说:'nbuff [0] == buff'将成立。 – 2013-03-08 17:13:39

1

2 in buff.Split(delim,2)指定要返回的最大子字符串数。如果在由delim中定义的字符分隔的字符串中有4个部分,则会发现有所不同。如果您使用Split(delim,2),则只会返回2个子字符串。

您也可以通过this page on MSDN阅读。

+0

好的,但困扰我的是为什么nbuff = buff.Split(delim);不会被使用,因为它会产生与nbuff = buff.Split(delim,2)相同的结果;在这种情况下,看起来像“2”是多余的? – 2013-03-08 19:54:45

2

2表示要返回的最大字符串数。

有关完整信息,请参阅here

该参数被称为count。下面是相关的文字:

如果有超过计数子在这种情况下,第一 计数减1子在第一计数返回的返回值减1 元素,并且此 实例中的其余字符将返回到返回值的最后一个元素中。

+0

好的,但困扰我的是为什么nbuff = buff.Split(delim);不会被使用,因为它会产生与nbuff = buff.Split(delim,2)相同的结果;在这种情况下,看起来像“2”是多余的? – 2013-03-08 19:53:42

+0

是的,我同意。该程序没有明显的原因使用该特定的超载。 – 2013-03-08 20:40:33

3

它指定了要返回的最大子字符串数。

拆分(CHAR [],Int32)将

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array. A parameter specifies the maximum number of substrings to return.

有几个重载到String.Split()方法列出here

+0

好的,但困扰我的是为什么nbuff = buff.Split(delim);不会被使用,因为它会产生与nbuff = buff.Split(delim,2)相同的结果;在这种情况下,看起来像“2”是多余的? – 2013-03-08 19:52:55