string.Remove(indexNum) not working at all [not removing anything]

Hi Hi, probably something simple I’m missing and I should not be obsessing over this right now but I am.

I wrote up a little tooltip script which makes a popup appear when you hover anything with a separate script attached. Then the popup changes its text to match what is stored on the separate script instance, and ez pz tooltip engaged.

However, I cam currently trying to to get it worked out so the tooltip can only be so wide, before it wrap the text onto a new line. Currently this is all but achieved with this code. (Jank hobby code warning)

void displayCustom(string toDisplay)
{
        string editedString = toDisplay;
        int x = 0;
        int y = 0;
        int controlNum = 23;
        while (x < editedString.Length)
        {
            if (y >= controlNum)
            {
                for (int Zee = 0; Zee < controlNum; Zee++)
                {
                    if ( editedString[x - Zee].Equals(' '))
                    {
                        editedString.Remove(x - Zee, 1);
                        editedString = editedString.Insert(x - Zee, "
");
                        x += 4;
                        y = 0;
                        Zee = controlNum;
                    }
                }
            }
            else
            {
                y++;
                x++;
            }
        }
        displayText.text = editedString;
}

However, the line editedString.Remove(x - Zee, 1); does nothing. I’ve moved it around, tried to tell it to remove more than 1, and can confirm it’s doing truly nothing. And since it is doing nothing, I’m getting some annoying indentation on my popup.

without that line, if If I set the controlValue to say 7 and give the popup the line “Thanks for nothing”

it will display
Thanks
for
nothing

with the line it displays the same thing, so idk if I’m using remove() wrong or if there is a more-correct way of doing this but any help would be appreciated.

As per the Microsoft docs:

So you you need to get the return on the method like you do the line below.

editedString = editedString.Remove(x - Zee, 1);

eyyy ty I didn’t understand it returned a ‘new’ string, not edited the inputed string. saved a hobbiest a night of headaches. marked resolved.

1 Like

Strings are conceptionally immutable types, even though they are reference types. Strings can not be altered once they are created (unless some unsafe code is used that directly manipulates the underlying memory).

If you are working with .NET standard 2.1 (or .NET 5/6 if not in unity), I would recommend you to possibly revise the method using Span. string operations are expensive and each new string means new memory allocation.

If the method is only called once on certain events, this is not absolutely necessary, but if it is called in every frame and maybe also from several objects, you should consider it.

3 Likes

it is not called in any update method, nor can it be called from more than one object at a time. I have a tooltip GO which has its text change by this, then it pops up and moves around with the mouse based on input.mouseposition. But appreciate the extra insight.