I want to remove last three characters from a string:
string myString = \"abcdxxx\";
Note that the string is dynamic data.
Easy. text = text.remove(text.length - 3)
. I subtracted 3 because the Remove
function removes all items from that index to the end of the string which is text.length
. So if I subtract 3 then I get the string with 3 characters removed from it.
You can generalize this to removing a
characters from the end of the string, like this:
text = text.remove(text.length - a)
So what I did was the same logic. The remove
function removes all items from its inside to the end of the string which is the length of the text. So if I subtract a
from the length of the string that will give me the string with a
characters removed.
So it doesn't just work for 3, it works for all positive integers, except if the length of the string is less than or equal to a
, in that case it will return a negative number or 0.