Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在get set函数中拆分字符串_C#_String_Get_Split_Set - Fatal编程技术网

C# 在get set函数中拆分字符串

C# 在get set函数中拆分字符串,c#,string,get,split,set,C#,String,Get,Split,Set,我正试图找到一种方法来自动将传入的字符串拆分成一个解析良好的数组或列表。我的页面上有一个,可以用逗号或空格分隔。该将填充我的多选项”。然后,我希望get/set自动将其解析为字符串数组 我走对了吗 private string _options; public string[] multiChoiceOptions { get { this._options = splitString(thi

我正试图找到一种方法来自动将传入的字符串拆分成一个解析良好的数组或列表。我的页面上有一个
,可以用逗号或空格分隔。该
将填充我的
多选项”。然后,我希望get/set自动将其解析为字符串数组

我走对了吗

        private string _options;
        public string[] multiChoiceOptions
        {
            get {
                this._options = splitString(this._options);
                return this._options; 
            }

            set { 
                this._options = value; 
            }
        }

        public string[] splitString(string value)
        {
            string[] lines = Regex.Split(value, "\r\n");

            return lines;
        }

在您的代码中,将所有数组变量一次性放入字符串变量是不可能的

例如:在代码中存储数组的正确方法是

private string _options;
private string[] newArray; //declare new array for storing array.
    public string[] multiChoiceOptions
    {
        get {
            this.newArray= splitString(this._options);
            return this.newArray; 
        }

        set { 
            this.newArray = value; 
        }
    }

    public string[] splitString(string value)
    {
        string[] lines = value.split(","); //use String.Split

        return lines;
    }
我在控制台应用程序中使用您的代码

static void Main(string[] args)
    {
        string[] arrayEmpty = new string[]{}; //I use empty array cause I don't know what you want to do.
        multiChoiceOptions = arrayEmpty;
    }

    private static string _options = "samle,sample";
    private static string[] newArray;
    public static string[] multiChoiceOptions
    {
        get
        {
            newArray = splitString(_options);
            return newArray;
        }

        set
        {
            newArray = value;
        }
    }

    public static string[] splitString(string value)
    {
        string[] lines = value.Split(','); //use String.Split

        return lines;
    }

我将使用一对属性,一个用于原始选项列表,另一个用于已解析的数组。解析将在原始可选值的setter上完成

由于设置选项的正确方法是使用源选项字符串,因此解析后的数组不需要setter

   private string[] z_multiChoiceOptions = null;
    public string[] multiChoiceOptions
    {
        get
        {
            return this.z_multiChoiceOptions;
        }

    }

    private string z_Options = null;
    public string Options
    {
        get { return z_Options; }
        set { 
            z_Options = value;
            if (value != null)
                this.z_multiChoiceOptions = Regex.Split(value, "\r\n");
            else
                this.z_multiChoiceOptions = null;
        }
    }

我走对了吗?
我不知道。首先发布一个可编译的代码。为什么只需要两个方法来拆分字符串和存储字符串变量?请礼貌一点。非常感谢。