【问题标题】:C# String Split Method [closed]C#字符串拆分方法[关闭]
【发布时间】:2013-03-08 17:00:37
【问题描述】:

我正在学习一本书中的教程,但有一小段代码我不明白。代码来了:

// 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);中的“2”的作用是什么?

字符串无论如何都会被分成两半,对吧?

即使没有像nbuff = buff.Split(delim);这样的“2”,结果也是一样的。

【问题讨论】:

标签: c# string methods split


【解决方案1】:

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

Split(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

【讨论】:

  • 好的,但困扰我的是为什么 nbuff = buff.Split(delim);不使用,因为它会产生与 nbuff = buff.Split(delim,2); 相同的结果在这种情况下,看起来“2”是多余的?
【解决方案2】:

这是要返回的最大子字符串数。将其更改为 3 无效的原因是因为要返回的子字符串少于 3 个,因此,按照设计,它无论如何都会返回所有可用的子字符串。 如果有可能会返回 5 子字符串,例如,则只会返回第一个 3

您可以阅读有关String.Split() 方法here 的更多信息。

【讨论】:

  • 好的,但是即使是“1”或者即使省略结果也是一样的,那为什么还是“2”呢?
  • 从给定的上下文来看,添加2 似乎没有任何明确的原因,但由于它来自教科书,因此有可能在其他地方已经解释了原因(也许是为了准备下一章/代码 sn-p 什么的)。
  • @nenad 您是否使用输入名称时指定的分隔符之一?如果名称不包含指定的分隔符之一,则字符串不会被拆分,它将与原始字符串保持相同。换句话说:nbuff[0] == buff 将是真的。
  • @keyboardP 原因没有在教科书中给出,这就是为什么我在这里问的原因,因为我不清楚,因为从我的角度来看,即使省略 2 也是一样的。
  • @nenad - 我也看不出任何理由。我想象的最接近的事情是 Kenneth K 提到的,相同的代码可用于拆分某人的名字和姓氏。但是,就我所见,2 似乎是多余的。
【解决方案3】:

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

请参阅here 了解完整信息。

这个参数叫做count。以下是相关文字:

如果此实例中有超过 count 个子字符串,则第一个 count 减1 子串在第一个count 减1 返回值的元素,以及 this 中的剩余字符 实例在返回值的最后一个元素中返回。

【讨论】:

  • 好的,但困扰我的是为什么 nbuff = buff.Split(delim);不使用,因为它会产生与 nbuff = buff.Split(delim,2); 相同的结果在这种情况下,看起来“2”是多余的?
  • 是的,我同意。该程序没有明显的理由使用该特定重载。
【解决方案4】:

buff.Split(delim,2) 中的2 指定要返回的最大子字符串数。例如,如果字符串有 4 个部分,由delim 中定义的字符分隔,那么您会注意到不同之处。如果你使用Split(delim,2),只会返回2个子字符串。

this page on MSDN也可以通读。

【讨论】:

  • 好的,但困扰我的是为什么 nbuff = buff.Split(delim);不使用,因为它会产生与 nbuff = buff.Split(delim,2); 相同的结果在这种情况下,看起来“2”是多余的?
猜你喜欢
  • 1970-01-01
  • 2016-05-21
  • 2012-03-25
  • 2018-06-09
  • 2020-11-12
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
相关资源
最近更新 更多