Search code examples
c#replaceuppercaselowercase

C# replacing a string


I have one list with words and their replacing words, for example:

desk-->table etc.

So lets say if user write desk it will give result table but if user write Desk with capital D it will not do any change. I know how to ignore uppercase but then the world will be replaced with table where t is lowercase... I want the t to be uppercase. So if desk-->table and if Desk-->Table... How i can do that?


Solution

  • You could call the replace function a second time, the second time with the capital words.

    For example:

    string result = input.Replace ("desk", "table");
    result = result.Replace ("Desk", "Table");
    

    To get the first character of a string to uppercase is not very difficult. You could use this method:

    string lower = "desk";
    string upper = char.ToUpper(lower[0]) + lower.Substring(1);