Work Done by a Spring

How much work is done by a spring when its end is moved from one position xi to another xf? Because the force is not constant, the work done is:

W = if F dx

Substituting -kx for F gives W = - kx dx,

This gives:   W = - ½ kx2 | xi xf   = ½ kxi2 - ½ kxf2