Removing between "["and"]" of a string

Hello all

I am currently trying to display the name of my objects in game, but due to a external reason they have come up with numbers on the end and there a lot of these objects. example name : (item [0001])

To handle this I thought I could remove this data from the string but am struggling on how to do this, after trying around with strings I have manged to extract the number, I just need to remove it from the string my code as bellow:

string id = clickedObject.name;
		int firstBracket = id.IndexOf("[")+1;
		int lastBracket = id.IndexOf("]");
		int difference = lastBracket - firstBracket;

Im just sruggling how to remove the difference from the id.

You almost got it already. You just need to use the string.Remove method. This works for me:

string test = "Item [0001] Test";

int firstBracket = test.IndexOf('[');
int lastBracket = test.LastIndexOf(']');
int diff = lastBracket - firstBracket + 1;
test = test.Remove(firstBracket, diff);

Printing test just gets me “Item Test”, that is, with two spaces between “Item” and "Test in it. Meaning that above code removed exactly the brackets and piece of string between them. If you need to remove multiple spaces or spaces that end up trailing the ends of the strings afterwards, there are ways to do so, too.