Reputation: 21
I am trying to split a string
(input
)
"1#Artikel1|ArtikelN$2#Artikel1|Artikel2|ArtikelN$3#ArtikelN"
into a List<string>
(liste
) with expected result (simplified)
{
"Artikel1 , ArtikelN",
"Artikel1 , Artikel2 , ArtikelN",
"ArtikelN"
}
but I am getting the compile time error in the process:
cannot convert string[] to string
private List<Data> GetData(string input)
{
List<Data> liste = new List<Data>();
foreach (string section in input.Split('$'))
{
String[] tab = section.Split('#');
String[] parties = new string[20];
int i = 0;
for (int j = 1; j < tab.Length; j++)
{
parties[i] =tab[j].Split('|'); <- ERROR HERE
i++;
};
liste.Add(new Data(// insert data));
}
return liste;
}
How can I solve this? thanks in advance.
Upvotes: 1
Views: 89
Reputation: 186668
If I've understood you right (please, see comments),
i want :
Artikel1 , Artikel
in the parties array
You want to Split
and then Join
articles within chapter (let's put it like this) i.e.
string input = @"1#Artikel1|ArtikelN$2#Artikel1|Artikel2|ArtikelN$3#ArtikelN";
List<string> liste = input
.Split('$') // split into chapters
.Select(chapter => chapter // for each chapter:
.Substring(chapter.IndexOf('#') + 1) // get rid of 1#, 2# etc. prefix
.Split('|')) // Split by |
.Select(items => string.Join(" , ", items)) // Join back by " , "
.ToList();
Let's have a look
Console.Write(string.Join(Environment.NewLine, liste));
Outcome:
Artikel1 , ArtikelN
Artikel1 , Artikel2 , ArtikelN
ArtikelN
Upvotes: 1
Reputation: 53958
This line tab[j].Split('|')
would split the string in tab[j]
in an array of strings based on the delimiter |
. So you can't assign the result of this call to
parties[i]
, whose type is string.
Upvotes: 2