开发者

Replacing a part of string while keeping the rest intact?

I am making an asp.net web application (with C#) that allows the users to copy/paste their writing and then the application search the content for UK and US conventions for example

in UK car engine room cover is called "bonnet" while in US it is called "hood" so the program will advice the user to change the word from bonnet to hood if he/she is searching for US based conventions (user can switch between UK or US conventions using radio buttons). Conventions are stored in a database.

My question is it开发者_StackOverflow社区 is not always "bonnet" or "hood" sometimes the writers use words like "bonnets" or "hoods" so the addition of 's' in the end changes the word completely, how can I change the actual word while keeping the rest intact, like for example bonnets should be changed into hoods (keeping the 's' intact while changed the actual word) ?

Thanks.


I assume that you want to detect bonnet(s) with spaces after/before or end/start of the sting.

then I would suggest using Regular expressions.

consider this Regex: (?i)^(.+ )?(bonnet)(s?)( .+)?$

(?i) means case insensitive,

now test this code:

string replacedText = Regex.Replace("ssdf bonnets sdf sdf sdf ", @"(?i)^(.+ )?(bonnet)(s?)( .+)?$", "$1hood$3$4");

I hope I helped


try this

if(Convention=="UK")
{
  MyString = MyString.Replace("hood","bonnet");
}
else if(Convention=="US")
{
  MyString = MyString.Replace("bonnet","hood");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜