diem_L
diem_L

Reputation: 399

C# - use same code for different array length

For my code I have a text file which looks similar like this:

  1. Step1 - * First Move - * Second Move
  2. Step2 - * First Move - * Second Move - * Third Move
  3. Step3 - * First Move
  4. Step4 - * First Move - * Second Move - * Third Move - * Fourth Move

For reading the text file I use the following code:

void Update()
{
     readTextFileLines("fileName", "Step1")
}

[MenuItem("Tools/Read file")]
public void readTextFileLines(string fileName, string Step)
{
    foreach(string line in System.IO.File.ReadAllLines("Assets/Resources/"+fileName+".txt))
    {
        if(line.Contains(Step))
        {
            string[] separator = { "-" };
            string[] strList = line.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);

            //Now I want to show the string in a UI text field.
            //For this I used the following part:

            infoText.text = strList[1] + "\n" + strList[2]

            // So if I have e.g. Step3 I will receive an error.
            // If I have Step2 it will not show the "Third Move"
        }
    }
}

My question now is, how can I rewrite my code, that I do not get the index error?

My first test was with the following code:

for(int i = 1; i < strList.Length; i++)
{
    informationText += strList[i] + "\n";
}

infoText.text = informationText;

but with this code it added way to many lines to my UI text field.

This is how my textfield looks like:

enter image description here

Upvotes: 0

Views: 105

Answers (3)

Dennis Meissel
Dennis Meissel

Reputation: 2493

Updated method:

[MenuItem("Tools/Read file")]
public void readTextFileLines(string fileName, string Step)
{
    foreach (string line in System.IO.File.ReadAllLines("Assets/Resources/" + fileName + ".txt))
    {
        if (line.Contains(Step))
        {
            string[] separator = { "-" };
            string[] strList = line.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);

            infoText.text = string.Join("\n", strList.Skip(1));
        }
    }
}

Upvotes: 1

Robin Bennett
Robin Bennett

Reputation: 3231

I think your problem is that your code adds a new line at the end of each section. You can avoid it like this:

var thisBit = "";
for(int i = 1; i < strList.Length; i++)
{
    if (thisBit != "") thisBit += "\n";
    thisBit += strList[i];
}
informationText += thisBit;

Or you could use this, which replaces the loop:

informationText += string.Join(strList, "\n");

Or, as all the above basically replaces "-" with "\n", you could use:

infoText.Text = line.Replace("-", "\n");

To trim off the "StepX" part, you could do it like this:

var indexOfStar = line.IndexOf("*");
var moves = line.Substring(indexOfStar);
infoText.Text = moves.Replace("-", "\n");

Upvotes: 1

Anton Anpilogov
Anton Anpilogov

Reputation: 141

If I understand you correctly you want to limit your output to the tree lines, so you can combine your condition in loop:

        var moves = "a-b-c-d-e-f-g";
        var strList = moves.Split(new[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
        string informationText = "";

        for(int i = 0; i < strList.Length && i < 3; i++)
            informationText += strList[i] + "\n";

so your output will be like:

  > a
    b
    c

Upvotes: 0

Related Questions