Home > Enterprise >  How to read single line in Unity by using Split("\n")
How to read single line in Unity by using Split("\n")

Time:12-10

I am trying to read a .txt file in Unity by using TextMeshProInputField and cannot read single line by using Split("\n")

I am trying to compare a .txt file with a text that I get from an InputField. Yet, by using the following code

lines = textField.text.Split("\n");

I cannot read a single line since I do not have any new line string in the input field.

Here is my code for comparing Input Field with the .txt file I have.

`

        for (int j = originalData.Count - 1; j > -1; j--)
        {
            for (int i = 0; i < ReadTXT.readTXT.originalFile.Count; i  )
            {
                if (ReadTXT.readTXT.originalFile[i]==originalData[j])
                {
                    Debug.Log("Original file is"   ReadTXT.readTXT.originalFile[i]   "Removing file is"   originalData[j]);
                }
            }

        }

`

CodePudding user response:

You can use ReadAllLines to read all lines of the file:

 string[] lines = File.ReadAllLines(@"file.txt");

 foreach(string line in lines)
 {
    //DoSomething(line);
 }

CodePudding user response:

If I understood your question correctly. you are looking for something like this.

Read the file, split the content by a new line character and then compare each line with textbox text.

string str = File.ReadAllText("yourFileName");

var lines = str.Split(Environment.NewLine);
foreach ( var line in lines )
{
    if(textField.text == line)
    {

    }
}

you can also use ReadAllLines which gives the string[] with each line as item

  • Related