开发者

VB.NET match whole word and replace

开发者 https://www.devze.com 2023-04-09 19:32 出处:网络
I\'m trying 开发者_StackOverflow社区to replace a string with another but the problem is that the string is matching some other string partially.

I'm trying 开发者_StackOverflow社区to replace a string with another but the problem is that the string is matching some other string partially.

For e.g. -

Dim x as String = "I am Soham"
x = x.Replace("am","xx")

After this replace I would only like the word am to replaced with xx but because my name also contains am its also getting replaced.

The value of x is I xx Sohxx. How can I stop this. Please help.


Use Regex.Replace and use the regular expression \bam\b. In a regular expression \b means "word boundary".


You can write up like as follows also.

X = X.Replace(" am"," xx")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号