0

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

5
  • Your "first test" looks correct. Can you tell me, how your output has to look like? Commented Jan 13, 2020 at 9:30
  • You should check what is the length of strList before trying to access items from it.. Commented Jan 13, 2020 at 9:31
  • you could also use foreach (var VARIABLE in COLLECTION) Commented Jan 13, 2020 at 9:33
  • @DennisMeissel I added the output as image. Unfortunately I just saw, that I forgot to write that I call de readTextFileLines in Update() Commented Jan 13, 2020 at 9:33
  • Maybe your informationText variable declared outside of the loop and it gets bigger each iteration? Commented Jan 13, 2020 at 9:38

3 Answers 3

1

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));
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this works also perfectly .. thanks a lot. I will use this code, because it is much shorter
1

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");

3 Comments

I used the infoText.Text = line.Replay(...) which worked fine, but now I have the 1. Step1also in my text field :/
I've added a bit to show how to remove the 'stepX' part.
I tried it with var thisBit = "" this works perfect .. thanks a lot
0

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

1 Comment

unfortunately not, I want to output all lines (each "-" marks a new line).My code should see if there are 2, 3, 4, 5 or more lines for my text field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.