By Joanne Chiu

China's yuan strengthened to levels last hit in March, as confidence builds that the country's economy is shaking off the coronavirus pandemic, and as local stock markets soar.

On Thursday, the currency traded at better than 7 to the dollar in both the tightly controlled onshore market and freer offshore markets in Hong Kong and elsewhere.

Craig Chan, head of global foreign-exchange strategy at Nomura, said a weaker dollar, China's improving economic outlook and the limited nature of recent Trump administration actions against China had all helped buoy the currency.

"One thing that tipped the scale was a quite substantial pickup in portfolio inflows into China," Mr. Chan added, referring to increased purchases of Chinese stocks and continued buying of Chinese bonds by foreign investors.

Recent survey data has shown economic activity gathering momentum, suggesting Beijing's uncompromising approach to the coronavirus pandemic is starting to pay dividends.

Some economists expect data due next week to show China's economy grew in the second quarter, after a 6.8% contraction in the first. Larry Hu, chief China economist at Macquarie Capital Ltd., estimates gross domestic product will be up 3% from a year earlier.

"It's a V-shaped recovery, but the question remains how resilient it's going to be in the second half," he said. He expects full-year growth of 2%.

Share indexes in China, such as the Shanghai Composite and the CSI 300, have surged to multiyear highs in recent days.

Foreign investors bought a net 55 billion yuan ($7.85 billion) of mainland stocks in the first five trading days of July through the Stock Connect trading link in Hong Kong, Wind data shows. That exceeds the total for all of last month.

Ken Cheung, chief Asian foreign-exchange strategist at Mizuho Bank in Hong Kong, said if China's government succeeds in its aim of fostering a "healthy bull market," the yuan could trade around 6.95 per dollar in the near term. But if the U.S. imposes financial sanctions on China or on Hong Kong banks, he said, the yuan could weaken.

Further out, Mr. Chan at Nomura said if Joe Biden wins November's presidential election it could spur a further rally in the yuan, given the presumptive Democratic nominee's approach to China would likely differ from President Trump's.

As of midafternoon Thursday, the onshore yuan stood at 6.9895 to the dollar, while the offshore yuan was at 6.9890. After years of preventing the yuan from weakening beyond 7 to the dollar, Beijing let it cross that threshold as trade tensions tightened last summer--with the central bank saying the passage wasn't irreversible. Since then the yuan's value has ranged between roughly 6.85 and 7.20 to the dollar.

Write to Joanne Chiu at joanne.chiu@wsj.com