The redshifted 21 cm signal from neutral hydrogen (HI) is potentially a very powerful probe for cosmology, but a difficulty in its observation is that it is much weaker than foreground radiation from the Milky Way as well as extragalactic radio sources. The foreground radiation at different frequencies are however coherent along one line of sight, and various methods of foreground subtraction based on this property have been proposed. In this paper, we present a new method based on the Robust Principal Component Analysis (RPCA) to subtract foreground and extract 21 cm signal, which explicitly uses both the low-rank property of the frequency covariance matrix (i.e. frequency coherence) of the foreground and the sparsity of the frequency covariance matrix of the 21 cm signal. The low-rank property of the foregrounds frequency covariance has been exploited in many previous works on foreground subtraction, but to our knowledge the sparsity of the frequency covariance of the 21 cm signal is first explored here. By exploiting both properties in the RPCA method, in principle, the foreground and signal may be separated without the signal loss problem. Our method is applicable to both small patch of sky with the flat-sky approximation, and to large area of sky where the sphericity has to be considered. It is also easy to be extended to deal with more complex conditions such as sky map with defects.