Cannot convert string to char

I’ve had a look round but nothing i see seems to answer. Here’s my code to start with;

    void readDictionary()
    {
        string[] data = wordData.text.Split(new string("," , "\n"), StringSplitOptions.None);

        int tableSize = data.Length / 3 - 1;
        MyWordList.words = new Words[tableSize];

        for(int i = 0; i < tableSize; i++)
        {
            MyWordList.words[i] = new Words();
            MyWordList.words[i].word = data[3 * (i + 1)];
            MyWordList.words[i].assignment = data[3 * (i + 1) + 2];
            MyWordList.words[i].meaning = data[3 * (i + 1) + 3];
        }
    }

So the idea is that I’m importing a spreadsheet which is three columns wide. This is meant to be like a dictionary of sorts (So there’s the word itself, a letter assignment for when I call the word into the game and a short definition which comes into player later) but for the life of me I can’t figure this out.
The three are stored as strings but when I use the Split code above, it comes back saying ‘cannont convert string to char’
I know I need a char or something in the Split code but apparently it won’t take two char’s.

Anything anyone can suggest to help would be appreciated.

For the curious I was following this tutorial;

That function takes a single character (char data type), but if you use “double quotes” (even around a single letter) it sends a string. Use single quotes ‘,’ instead.

How do I get it to recognise I want it to split via ‘new line’? It won’t take /n.

The escape character in C# strings/chars isn’t /, it is .

char c = '\n';

To be clear I’ve tried

string[] data = wordData.text.Split(',', '\n');

But that doesn’t work as it does this;

I’m trying to get it so the Word is one word, the Assignment is one letter and the meaning is a string of X length

What do you mean?

What is your input data? Where does ‘wordData’ come from and how is it formatted?

I’ll keep it simple and show the whole code.
I’m puling wordData from a CSV file where I have a list of words stored. it’s a spreadsheet with three columns.

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class Dictionary : MonoBehaviour
{
    public TextAsset wordData;

    [System.Serializable]

    public class Words
    {
        public string word;
        public string assignment;
        public string meaning;
    }

    [System.Serializable]

    public class WordList
    {
        public Words[] words;
    }

    public WordList MyWordList = new WordList();

    // Use this for initialization
    void Start()
    {
        readDictionary();
    }
    void readDictionary()
    {
        string[] data = wordData.text.Split;

        int tableSize = data.Length / 3 - 1;
        MyWordList.words = new Words[tableSize];

        for(int i = 0; i < tableSize; i++)
        {
            MyWordList.words[i] = new Words();
            MyWordList.words[i].word = data[3 * (i + 1)];
            MyWordList.words[i].assignment = data[3 * (i + 1) + 2];
            MyWordList.words[i].meaning = data[3 * (i + 1) + 3];
        }
    }

}

Ok so I figured it out
Broke it down a bit and realised I could just do it this way;

string[] delimiterChars =
        {
            "," ,
            "\r\n"
        };

        string[] data = wordData.text.Split(delimiterChars, StringSplitOptions.None);

Hopefully this will help you too.

The method you’re using is a little bit overcomplicated to be honest. I would just use a StringReader to read the text line by line, then process each line individually by splitting it, something like this:

using (var reader = new StringReader(wordData.text))
{
  string line = null;

  while ((line = reader.ReadLine()) != null) // Will read the text line by line
  {
    var splittedLine = line.Split(' ');
    var words = new Words { word = splittedLine[0], assignment = splittedLine[1], meaning = splittedLine[2] };
    // do whatever you want with your "words" class...
  }
 }
2 Likes

Beside all the noise about char and string conversion, you do realise that this does not make much sense, right?

            MyWordList.words[i].word = data[3 * (i + 1)];
            MyWordList.words[i].assignment = data[3 * (i + 1) + 2];
            MyWordList.words[i].meaning = data[3 * (i + 1) + 3];

You said that you have 3 columns and you do multiply by 3. However you grab the “word” from offset 0, the “assignment” from offset 2 and the “meaning” from offset 3 which would imply a 4 column layout where the second column is ignored all the time. If you actually have 3 columns, you would always grab the wrong assignment and meaning. Your meaning is always the “word” of the next line. 3 * (i+1)+3 is the same as 3*(i+2).

Like Nad_B said, you should properly parse the file to begin with. If any line does not have exactly 3 columns you would get completely out of sync. So if one line is missing the meaning or if there’s another 4th column, everything would break down. That’s not really a robust way of parsing one of the simplest data formats out there :slight_smile:

Shortly after changing my code like in my last post, I realised the same thing so I changed it and it works the way I want to. I didn’t mention it in my reply because I realised the offset was wrong after I hit reply. Didn’t think to mention it.
Like I said I was following a tutorial and the person explaining it was using the offset to avoid taking the top row which is the header. His worked in the video which is why I was confused as to why mine didn’t work.