The characteristic function for Jacobi matrices with applications