- Home /
Am I comparing strings the right way? (I am making a text box for interaction)
Hi guys. I'll try to be as specific as I can. I have the following text on a .txt:
This is a tree.
This is a tree.
This is STILL a tree.
...
Really?
THIS IS A TREE FOR THE LAS TIME!
I break the File into lines by using
textLines = (textFile.text.Split ('\n'));
And I start navigating the textlines array. By using enter I advance to the next line, if it has text it writes it down on the Text Box, if it doesn't it should disbale the text box and the manager. The code for that is this.
// Update is called once per frame
void Update () {
//If we haven't passed the last line set current line to the actual line we are on
//And if we can write whe call the Writing Coroutine, but not before setting canWrite to false
//So that it cannot be accesed until it is done writing the current line
if (currentLine <= lastLine) {
currentTextLine = textLines [currentLine];
if (currentTextLine == "") {
print ("Empty Line");
TurnOFF ();
}
if (canWrite == true) {
canWrite = false;
StartCoroutine ("WriteText");
}
//If enter is pressed and the testbox is already initializaed
//We completly write the current line instantly if it hasn't finished
//If it has finished it advances to the next line
if (Input.GetKeyDown (KeyCode.Return) && initialized == true) {
NextLine ();
}
//currentText.text = textLines [currentLine];
} else{
TurnOFF ();
}
}
However if (currentTextLine == "") is never entered, Whenver I get to an empty line the text box and manager stay ON, it writes nothing but they stay ON. The only time it is entered is when I reach the last line which is an empty one. Why does it enters it when it is the last one but not on the others?
My TurnOFF function is.
void TurnOFF()
{
StopCoroutine ("WriteText");
textBox.SetActive (false);
this.gameObject.SetActive (false);
}
Why don't you simply output currentTextLine with a simple Debug.Log? Try this ins$$anonymous$$d :
if ( string.IsNullOrWhiteSpace(currentTextLine) )
Answer by Minuks · Dec 10, 2017 at 10:38 PM
The empty lines are actually not empty, they contain a line feed (\n), and possibly a return carriage (\r).
You may want to try if (currentTextLine .Replace("\r", "").Length == 0)
Because he splits using textFile.text.Split ('\n') the \n character should not be present in the string (However, \r may be here)
Good point, I edited my answer based on your comment
Thanks, I'm not on my PC right now but I will try it.
Your answer
Follow this Question
Related Questions
Textboxes and server side storage 1 Answer
Text and texture in GUI? 1 Answer
How to disable the keyboard text box on Android? 0 Answers
Help setting up a game manager 1 Answer
Problem with writing text in a box - typewriter style 1 Answer