替换两个字符串 [英] Replacing Two Strings

查看:122
本文介绍了替换两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个字符串..

I have 2 strings..

string firstword="Hello How Are You";
string correct=firstword.Replace("hello","h");


现在,我想替换firstword weather,第一个单词为大写或小写,如果匹配这两个相同或不同的单词,则直接替换为... ????
有没有人有任何想法.. ????
有什么方法可以添加ignorecase ...


Now I want to replace firstword weather the first word is in upper case or lower it matches weather these both are same or not and then simply replaces...????
Does anyone have any ideas..????
Is there any way for adding ignorecase ...

推荐答案

Kaiwa的答案将具有将字符串的其余部分更改为小写的效果,这可能不会成为您想要的行为.

正则表达式 [ ^ ]是实现所需内容的好方法:

Kaiwa''s Answer will have the effect of changing the rest of the string to lower case, this might not be the behaviour you want.

Regular Expressions[^] are a good way to go about what you want:

string regularExpressionPattern = "zzz"
Regex regex = new Regex(regularExpressionPattern, RegexOptions.IgnoreCase);
string foo = "zZz this is sample text";
string bar = regex.Replace(foo, "Goodbye");




请注意,我的代码只是替换了"zzz",而忽略了大小写.如果要替换第一个单词(而不只是"hello"),则可以创建一个正则表达式来执行此操作.显然,无论哪种情况,您都需要自己确定模式:-).




Note that my code just replaces "zzz", ignoring the case. If you want you replace the first word (rather than just "hello") you can create a regular expression to do this. Obviously, in either case, you''ll need to work out the pattern for yourself :-).


感谢答案
但是如果我不知道天气,如果字符串是从数据库den生成的,则字符串是大写还是小写bcz,我有什么办法设置ignorecase?
thanx for the answer
but if i dont know weather the string is in upper case or lower case bcz if the string is being generated from database den do i have any way to set ignorecase?


这篇关于替换两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆