2013-03-08 56 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