Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

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

Discussion in 'Scripting' started by SomeVVhIteGuy, Sep 6, 2022.

  1. SomeVVhIteGuy


    Mar 31, 2018
    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)

    Code (CSharp):
    1. void displayCustom(string toDisplay)
    2. {
    3.         string editedString = toDisplay;
    4.         int x = 0;
    5.         int y = 0;
    6.         int controlNum = 23;
    7.         while (x < editedString.Length)
    8.         {
    9.             if (y >= controlNum)
    10.             {
    11.                 for (int Zee = 0; Zee < controlNum; Zee++)
    12.                 {
    13.                     if ( editedString[x - Zee].Equals(' '))
    14.                     {
    15.                         editedString.Remove(x - Zee, 1);
    16.                         editedString = editedString.Insert(x - Zee, "<br>");
    17.                         x += 4;
    18.                         y = 0;
    19.                         Zee = controlNum;
    20.                     }
    21.                 }
    22.             }
    23.             else
    24.             {
    25.                 y++;
    26.                 x++;
    27.             }
    28.         }
    29.         displayText.text = editedString;
    30. }
    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

    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.
  2. spiney199


    Feb 11, 2021
    As per the Microsoft docs:
    So you you need to get the return on the method like you do the line below.
    Code (CSharp):
    1. editedString = editedString.Remove(x - Zee, 1);
  3. SomeVVhIteGuy


    Mar 31, 2018
    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.
    spiney199 likes this.
  4. Bunny83


    Oct 18, 2010
    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).
  5. runner78


    Mar 14, 2015
    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<T>. 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.
    SomeVVhIteGuy, Anthiese and Bunny83 like this.
  6. SomeVVhIteGuy


    Mar 31, 2018
    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.