user188237
user188237

Reputation:

Replace strings in file

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

Upvotes: 1

Views: 364

Answers (3)

maxy
maxy

Reputation: 438

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

Upvotes: 0

danish
danish

Reputation: 5600

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.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062510

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";

Upvotes: 1

Related Questions