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?
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);
You are saying that you have a list with words and their replacing words. So the data structure will be
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("desk","table");
dict.Add("Desk","Table");
If this is correct, then the following will work
var result = dict["Desk"];
But if you are maintaining the values in the below way,
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("desk","table");
then the solution may be
private void button1_Click(object sender, EventArgs e)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("desk","table");
string input = "Desk";
var dictValue = dict[input.ToLower()];
var result = IsInitCap(input.Substring(0, 1))
? System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(dictValue)
: dictValue;
}
private bool IsInitCap(string str)
{
Match match = Regex.Match(str, @"^[A-Z]");
return match.Success ? true : false;
}
Hope this helps
You can use the below code to make the first letter of the input string to UpperCase,
str = str.First().ToString().ToUpper() + String.Join("", str.Skip(1));
Now in your case, use the Dictionary data structure to store data.
Store the input value as such (key)desk->table(value)
Now use the above code and make the first letter uppercase and store it (Desk->Table)
So you can now get the values as if desk-->table and if Desk-->Table.
This always fetches the value in time complexity O(1) by compromising the space complexity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.