List to array

Hi,

I am making an alternative split string component and I try to use the String.Split method (https://docs.microsoft.com/en-us/dotnet/csharp/how-to/parse-strings-using-split). I need to convert a list of strings (my input splitting characters) to an array of characters.

How can I do this best?

Gr,

Peter

Iterate over the strings, call ToCharArray() on them all and append those to a list of chars.

Do note that individual chars may be combining symbols and not standalone characters in their own right. If you care about that sort of thing you need to split your strings into logical character strings which is a bit harder.

I am stuck at …append those to a list of chars. Could you give me an additional suggestion?

        /// Declare a variable for the input string and seperation characters.
        string stringOriginal = null;
        var stringList = new List<string>();
        
        /// Check and store input values in the placeholders.
        bool success1 = DA.GetData(0, ref stringOriginal);
        bool success2 = DA.GetData(1, ref stringList);

        /// Convert list of strings to array of characters
        List<Char> listOfChars;
        
        for (int i = 0; i < stringList.Count; i++)
        {
            Array chars = stringList[i].ToCharArray();
        }

I was planning to check somehow that the input list contains single symbols, but I will do that after this.

var strings = some collection of strings.
var chars = new List<char>();
foreach (var s in strings)
  chars.AddRange(s.ToCharArray());

Incidentally, if you only want the unique chars from a collection of strings and you don’t care about their order, use HashSet<char> to store them.

using System;
using System.Linq;
using System.Collections.Generic;
public class Utility {
    public char[][] ToCharacters(IEnumerable<string> strList) {
        return strList.Select(str => str.ToArray()).ToArray();
    }
}

If you want unique chars:

using System;
using System.Linq;
using System.Collections.Generic;
public class Utility {
    public char[] ToUniqueCharacters(IEnumerable<string> strList) {
        return strList.SelectMany(c => c).Distinct().ToArray();
    }
}

Thank you @DavidRutten and @gankeyu. I am getting closer.