不能隐式地将类型“char[]”转换为“string[]”

时间:2022-05-01 16:05:36

strArr is the complete string which I am splitting to get different titles n date but i want them in a string array. the moment I apply . ToArray() {in string[] titl = item.Title.ToArray();}

strArr是一个完整的字符串,我将其拆分以获得不同的标题n个日期,但我希望它们在一个字符串数组中。当我申请的时候。ToArray() {in string[] titl = item.Title.ToArray();}

the error message

错误消息

{Cannot implicitly convert type 'string' to 'string[]'} changes to {Cannot implicitly convert type 'char[]' to 'string[]'}

{不能隐式地将类型'string'转换为'string[]'}更改为{不能隐式地将类型'char[]'转换为'string[]'}

. Everything in class Data is also of string type

。类数据中的所有内容也是字符串类型

public class Data
{

    public string Date
    {
        get;
        set;
    }

    public string Title
    {
        get;
        set;
    }
}

string xml1 = Search();

string[] strArr = xml1.Split('|');

string[]strArr = xml1.Split(“|”);

    int i,j;
Data item = new Data();

 for (i = 0; i < strArr.Count(); i = i + 2)

        {
            if (strArr[i].ToString() != "")
            {
                item.Title = strArr[i];
                string[] titl = item.Title.ToArray();
            }
        }
        for (j = 1; j < strArr.Count(); j = j + 2)
        {
            item.Date = strArr[j];
            string[] Dat = item.Date.ToArray();

        }

3 个解决方案

#1


1  

string.ToArray() returns a char type array.

toarray()返回一个char类型数组。

As you need all title and dates you should have an array of type Data:

当您需要所有的标题和日期时,您应该有一个类型数据数组:

string xml1 = Search();
string[] strArr = xml1.Split('|');

int totalitems = strArr.Length / 2; //get Titles count
Data[] items = new Data[totalitems];    //initialize array of data

for (int i = 0; i < strArr.Length; i = i + 2)
{
    //new instance of Data
    if(items[i/2] == null)
        items[i/2] = new Data();

    if (strArr[i].ToString() != "") //get title
    {
        items[i/2].Title = strArr[i];
    }
    if (strArr[i + 1].ToString() != "") //get date
    {
        items[i/2].Date = strArr[i + 1];
    }
}

You don't need 2 for loops anymore.

你不再需要2个for循环。

#2


0  

The ToArray method on string gives you character array. You can select character as a string with linQ Select method.

字符串上的ToArray方法为您提供字符数组。您可以使用linQ select方法将字符选择为字符串。

string[] titl = item.Title.Select(c=>c.ToString()).ToArray();

You probably need to re-think that if you want the string array or character array as if you want the character array then you need to change the declaration of the title array from string to char.

您可能需要重新考虑,如果您想要字符串数组或字符数组,就像您想要字符数组一样,那么您需要将标题数组的声明从字符串更改为char。

 char[] titl = item.Title.ToArray();

#3


0  

Calling .ToArray() on a string returns char[], not string[]. There is not implicit conversion between those two types.

在字符串中调用. toarray()返回char[],而不是string[]。这两种类型之间不存在隐式转换。

To make this work try this:

要完成这项工作,请尝试以下方法:

string[] titl =
        item.Title.ToCharArray().Select(c => c.ToString()).ToArray();

Per your comment below, try this:

根据你的评论,试试这个:

var xml1 = "X|A|Y|B|Z|C|W|D";
var strArr = xml1.Split('|');

var items =
    strArr.Where((x, n) => n % 2 == 0)
        .Zip(
            strArr.Where((x, n) => n % 2 == 1),
            (t, d) => new Data()
            {
                Title = t,
                Date = d
            })
        .ToArray();

I get this result:

我得到这个结果:

不能隐式地将类型“char[]”转换为“string[]”

#1


1  

string.ToArray() returns a char type array.

toarray()返回一个char类型数组。

As you need all title and dates you should have an array of type Data:

当您需要所有的标题和日期时,您应该有一个类型数据数组:

string xml1 = Search();
string[] strArr = xml1.Split('|');

int totalitems = strArr.Length / 2; //get Titles count
Data[] items = new Data[totalitems];    //initialize array of data

for (int i = 0; i < strArr.Length; i = i + 2)
{
    //new instance of Data
    if(items[i/2] == null)
        items[i/2] = new Data();

    if (strArr[i].ToString() != "") //get title
    {
        items[i/2].Title = strArr[i];
    }
    if (strArr[i + 1].ToString() != "") //get date
    {
        items[i/2].Date = strArr[i + 1];
    }
}

You don't need 2 for loops anymore.

你不再需要2个for循环。

#2


0  

The ToArray method on string gives you character array. You can select character as a string with linQ Select method.

字符串上的ToArray方法为您提供字符数组。您可以使用linQ select方法将字符选择为字符串。

string[] titl = item.Title.Select(c=>c.ToString()).ToArray();

You probably need to re-think that if you want the string array or character array as if you want the character array then you need to change the declaration of the title array from string to char.

您可能需要重新考虑,如果您想要字符串数组或字符数组,就像您想要字符数组一样,那么您需要将标题数组的声明从字符串更改为char。

 char[] titl = item.Title.ToArray();

#3


0  

Calling .ToArray() on a string returns char[], not string[]. There is not implicit conversion between those two types.

在字符串中调用. toarray()返回char[],而不是string[]。这两种类型之间不存在隐式转换。

To make this work try this:

要完成这项工作,请尝试以下方法:

string[] titl =
        item.Title.ToCharArray().Select(c => c.ToString()).ToArray();

Per your comment below, try this:

根据你的评论,试试这个:

var xml1 = "X|A|Y|B|Z|C|W|D";
var strArr = xml1.Split('|');

var items =
    strArr.Where((x, n) => n % 2 == 0)
        .Zip(
            strArr.Where((x, n) => n % 2 == 1),
            (t, d) => new Data()
            {
                Title = t,
                Date = d
            })
        .ToArray();

I get this result:

我得到这个结果:

不能隐式地将类型“char[]”转换为“string[]”