We introduce a new technique for isolating components on the spectral side of the trace formula. By applying it to the Jacquet--Rallis relative trace formula, we complete the proof of the global Gan--Gross--Prasad conjecture and its refinement Ichino--Ikeda conjecture for U(n)×U(n+1) in the stablecase.