tags:

views:

152

answers:

3

Hi, I have to replace in a following manner

if the 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

+1  A: 

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";
Marc Gravell
A: 

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.

danish
yah sure..but how to make next character to be upper case.ie _c will have to be _sC.I am a newbie..
sajad
yourString.Replace("_c", "_sC") would do that.
danish
If you read the question the search term isn't fixed so this won't exactly to it. He will need a regex.
James
I guess it will. If all strings are like "string _something", he needs stringVariable.Replace("string _", "string _s"
danish
A: 
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

maxy