user3010406
user3010406

Reputation: 579

Remove Last Element In Array, Insert New

I have a string[] with the following values in it:

x
y
z

I am trying to remove the string z, based on the fact that is the last element in the array, and replace it with w. Is this possible?

Upvotes: 1

Views: 192

Answers (2)

Nin-ya
Nin-ya

Reputation: 252

Possible. If your array has fix 3 item.

Array starts at 0 index, so z is at the 2 index.

arr[2] = "w";

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61349

You can use Length to find the last index, and just assign it to replace it:

myArray[myArray.Length - 1] = "w";

Note the -1 since Length is not 0-indexed

Upvotes: 8

Related Questions