Home > OS >  Large Unicode List to Char[] c#
Large Unicode List to Char[] c#

Time:09-25

I pass a string containing separate characters on each line to a Unicode list with this code.

string MultiLineCharArray = string.Join(Environment.NewLine, CharArray);
var UnicodeList = MultiLineCharArray.Select(str => Convert.ToUInt16(str)).ToList();

when reversing it the program dies, it does not even try, very badly:

for (int i = 0; i < UnicodeList.Count; i  )
{
      MultiLineCharArray = string.Join(Environment.NewLine, Convert.ToChar(UnicodeList[i]));
}

I need the MultiLineCharArray to then convert it into an Array of its valid Unicode characters (unicode = A) going through each line to convert it to a single string. The Unicode list is very long (9,000) elements, maybe that's why the program crashed, is there a more optimal way to do it?

CodePudding user response:

Use LINQ and String functions

// to populate charArray with dummy chars A through J
var charArray = Enumerable.Range(65, 10).Select(i => (char)i);
// your existing code
var multiLineCharArray = string.Join(Environment.NewLine, charArray);
var unicodeList = multiLineCharArray.Select(str => Convert.ToUInt16(str)).ToList();
// to reverse
var multiLineCharArray1 = new string(unicodeList.Select(u => (char)u).ToArray());
var charArray1 = multiLineCharArray1.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  • Related