-1

I get an error in Visual Studio: 'char' does not contain a definition for split, and no extension method 'Split' accepting a first argument of type 'char' could be found (are you missing a using directive or an assembly reference?)

This is my code:

    private void BuildData(string data)
    {
        var endOfText = "b4";

        if (data.EndsWith(endOfText))
        {
            this.lastMessage.Append(data);

            Parse(this.lastMessage.ToString());

            this.lastMessage.Clear();
        }
        else
        {
            this.lastMessage.Append(data);
        }
    }

    public static void ParseLength(string text)
    {
        const int skillId = 1;
        const int queueId = 2;

        var skills = text[skillId].Split('\t');  // Split error
        var queueLengths = text[queueId].Split('\t'); // Split error

        var res = new SkillColl();

        for (var i = 0; i < skills.Length; i++)
        {
            try
            {
                var skill = Int32.Parse(skills[i]);
                var queueLength = Int32.Parse(queueLengths[i]);

                result.Add(skill, queueLength);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
        queueLengths = res;
    }

how to parse this two value the most optimised way, without writing the whole code again:

 var skills
 var queueLengths
Peter Smith
  • 47
  • 2
  • 7
  • 2
    You are trying to split a char and not a string. text[skillId] is a char located at skillid position in text string. – nevsv Apr 05 '17 at 14:04

2 Answers2

1

string[indexer] returns the specific char that's located in that specific index.
Perhaps you mean to use text.Split('\t')[skillId], that would give you the second element in the string array returned by the Split method. However, I would suggest using a better storage for data then a formatted string.

ATC
  • 907
  • 1
  • 9
  • 14
0

ATC is correct in that you haven't split your string yet before you index.

var skills = text[skillId].Split('\t');

You try to pull the index on a string, which is too early, you haven't split it yet:

var skills = text.Split('\t')[skillId];

This will give you the indexable array after the split.

var skills = text.Split('\t')[skillId];  // Split error
var queueLengths = text.Split('\t')[queueId]; // Split error
  • okay, i changed the order. now i get in the try part that var skill = Int32.Parse(skills[i]); cannot convert char to string – Peter Smith Apr 05 '17 at 14:13
  • var skill = Int32.Parse(skills[i].ToString()); – Thomas Bright Apr 05 '17 at 14:20
  • No reason to parse though, chars can safely be casted to integers, like so: `int x = (int)ch;` – Malte R Apr 05 '17 at 14:33
  • @Malte R, that would return ASCII value of that particular character, not the actual value it stands for. Example: for character '9', cast to int would return 57, not 9. I don't think he needed ASCII code here. – Djuka Apr 06 '17 at 06:15
  • @Djuka I think you are right about him needing the "actual" value and not the ASCII value – Malte R Apr 07 '17 at 23:08