String.Remove not working

Hi!

I’m working on a dialogue system and am having some frustrating trouble with String.Remove. I got it working fine in one part of the script, but now that I’m trying to utilize it again it won’t work at all. All ‘┐’ characters are supposed to be removed when clicking Z to make the text scroll by instantly. It works when the text scrolls by with normal speed in the coroutine, but not here. I’ve put Debug.Log(a); to check if it actually catches every instance of the ‘┐’ character, and it does!

I’m sure there’s something crucial I’m not seeing and I’d really appreciate any help I could get. Will provide more details if needed. :slight_smile:

if (Input.GetKeyDown(KeyCode.Z))
        {
            if (!isTyping)
            {
                currentLine++;

                if (currentLine > endAtLine)
                {
                    DisableTextBox();
                }
                else
                {
                    StartCoroutine(TextScroll(textLines[currentLine]));
                }
            }
            else if (isTyping && !cancelTyping)
            {
                int i = 0;
                string lineOfText = textLines[currentLine];

                while (i < lineOfText.Length)
                {
                    char a = lineOfText.ToCharArray()*;*

i++;

if (a == ‘┐’)
{
Debug.Log(a);
lineOfText = lineOfText.Remove(i, 1);
}
}
cancelTyping = true;
}
}
Below is an example of String.Remove working in the same script.
private IEnumerator TextScroll (string lineOfText)
{
int letter = 0;
int i = 0;
theText.text = “”;
isTyping = true;
cancelTyping = false;

while (isTyping && !cancelTyping && (letter < lineOfText.Length - 1))
{

char a = lineOfText.ToCharArray();
i++;

if (a == ‘┐’)
{
lineOfText = lineOfText.Remove(i - 1, 1);
yield return new WaitForSeconds(0.5f);
}

theText.text += lineOfText[letter];
letter++;

if ((a != ‘,’ && a != ‘.’ && a != ‘!’ && a != ‘?’ && a != ‘┐’) || ((a == ‘.’ || (a == ‘!’) || (a == ‘?’)) && i == (lineOfText.Length - 1)))
{
yield return new WaitForSeconds(typeSpeed);
}
else if (a == ‘,’)
{
yield return new WaitForSeconds(0.25f);
}
else if (a == ‘.’ || (a == ‘!’) || (a == ‘?’))
{
yield return new WaitForSeconds(0.5f);
}

}
theText.text = lineOfText;
isTyping = false;
cancelTyping = false;
}

Even “string” is a reference type it behaves like a value type as it’s “imutable”. So an instance of a string can never be changed.

Here you create a local variable:

string lineOfText = textLines[currentLine];

However at the end of your while loop you don’t do anything with your variable. If you want to change the current text line you have to add this after your while loop:

textLines[currentLine] = lineOfText;

ps: this is the worst thing you can do in a loop:

char a = lineOfText.ToCharArray()*;*

ToCharArray will create a new char array every time the method is called. A string can directly be indexed like this:
char a = lineOfText*;*

Thanks for the tip about the Arrays, I changed it and will keep it in mind for the future. I’m still not too proficient at programming so tips like these really help :smiley:

Unfortunately adding textLines[currentLine] = lineOfText; after the while loop didn’t change anything at all. Makes me think it has to do with something else entirely, but I have no idea what it could be :confused:

Changed version:

if (Input.GetKeyDown(KeyCode.Z))
        {
            if (!isTyping)
            {
                currentLine++;

                if (currentLine > endAtLine)
                {
                    DisableTextBox();
                }
                else
                {
                    StartCoroutine(TextScroll(textLines[currentLine]));
                }
            }
            else if (isTyping && !cancelTyping)
            {
                int i = 0;
                string lineOfText = textLines[currentLine];

                while (i < lineOfText.Length)
                {
                    char a = lineOfText*;*

i++;

if (a == ‘┐’)
{
Debug.Log(a);
lineOfText = lineOfText.Remove(i, 1);
}
}
textLines[currentLine] = lineOfText;
cancelTyping = true;
}
}