开发者

Replace strings in file

I have to replace in a following manner

if th开发者_Go百科e string is "string _countryCode" i have to replace it as "string _sCountryCode"

as you can see where there is _ I replace it with _s followd be next character in capitals ie _sC

more examples:

string _postalCode to be replaced as string _sPostalCode

string _firstName to be replace as string _sFirstName

Please help. Preferably answer in C# syntax


Not sure I understand why, but perhaps something like:

static readonly Regex hungarian =
        new Regex(@"(string\s+_)([a-z])", RegexOptions.Compiled);
...
string text = ...
string newText = hungarian.Replace(text, match =>
    match.Groups[1].Value + "s" +
    match.Groups[2].Value.ToUpper());

Note that the regex won't necessarily spot examples such as (valid C#):

string
    _name = "abc";


If the pattern of the strings are as you have shown, then you do not need to go for a regex. You can do this using Replace method of the string class.


StringBuilder ss=new StringBuilder(); 
         string concat="news_india";//or textbox1.text;          
         int indexs=concat.LastIndexOf("_")+1;//find "_" index 
         string find_lower=concat.Substring(indexs,1); 
         find_lower=find_lower.ToUpper(); //convert upper case 
         ss.Append(concat);
         ss.Insert(indexs,"s"); //s->what ever u like give "+your text+"
         ss.Insert(indexs+1,find_lower);

try this..its will work

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜