South African President Jacob Zuma has visited a refugee camp in the port city of Durban after a fresh outbreak of anti-foreigner violence.
Mr Zuma told those who had fled the violence that it went against South African values and that he would bring it to an end.
He was jeered by some in the crowd who accused him of acting too slowly.
At least six people have died in xenophobic attacks in Durban, with violence spreading to other areas.
Migrants, mostly from other African states and Asia, have moved to South Africa in large numbers since white-minority rule ended in 1994. Many South Africans accuse them of taking jobs in a country where the unemployment rate is 24%.
In a statement, Mr Zuma said: "These attacks go against everything we believe in. The majority of South Africans love peace and good relations with their brothers and sisters in the continent."
Then, as he visited the refugee camp in the Chatsworth area of Durban - after cancelling a trip to Indonesia - Mr Zuma said in a televised speech: "We are certainly going to stop the violence."
Addressing migrants who were planning to return to their home countries, he said: "Those who want to go home, when the violence stops you are welcome to return."
Mr Zuma said it was a minority that was causing trouble, but the BBC's Southern Africa correspondent, Karen Allen, says he was heckled by some in the crowd who branded his visit as too little, too late, adding that they were planning to leave South Africa altogether.
During the anti-immigration violence in the past fortnight, police have arrested 150 people for public-order related offences.
Several thousand foreigners have fled their homes to shelter in makeshift camps, and neighbouring Zimbabwe, Malawi and Mozambique have announced plans to evacuate citizens.
On Saturday, Zimbabwean President Robert Mugabe, speaking as chair of both the Southern African Development Community and African Union, said: "I would want now to express our sense of shock, disgust, as we abhor the incidences which happened in Durban."
Small groups attacking shops in several areas around Johannesburg on Saturday.
Police used rubber bullets to disperse looters in Alexandra, a township north of the city, with more than 30 people arrested.
Zulu King Goodwill Zwelithini has been accused of fuelling the attacks by saying that foreigners should "go back to their countries". However, he says his comments were distorted.
Official data suggests there are about two million foreign nationals in South Africa, about 4% of the total population. But some estimates put the number of immigrants at five million.