Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How to find closest string(s) in list:
var list = new List<string>
{
"hello how are you",
"weather is good today",
"what is your name",
"what time is it",
"what is your favorite color",
"hello world",
"how much money you got",
"where are you",
"like you"
};
and if updated input is:
string input = "how are you";
and another one with type error:
string input = "how are ytou";
For both cases would be good to get this:
hello how are you
where are you
or even this result:
hello how are you
where are you
how much money you got
or at least just:
hello how are you
I need it to avoid minimal type error in user request to make response.
A simple approach would be to use String.Compare to get the
lexical relationship between the two comparands
Order your available items after comparing with the input and take the best match like
string bestMacht = list.OrderBy(s => string.Compare(s, input)).First();
This is only the first approach because the order of words should be ignored. Let's improve this to a full solution. After splitting the strings
string[] splittedInput = input.Split(' ');
you are able to compare the single words using a IEqualityComparer. You are free to define how many characters are possible to fail every word (in this case 2).
private class NearMatchComparer : IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
return string.Compare(x, y) < 2;
}
public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
Use this comparer and compare the words of the input and your dictionary. If two words (define it like required) are matching (whatever order) select the string.
List<string> matches = list.Where(s => s.Split(' ')
.Intersect(splittedInput, new NearMatchComparer()).Count() >= 2)
.ToList();
The result is a list of potential matches.
I would use a Levenshtein distance. This gives you a value of how different strings are. Just choose the min distance of your set.
How to calculate distance similarity measure of given 2 strings?
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a List<string> with values SK-Disbur and SK_Recon. When I sort the list, what should be the output?
I am getting:
SK_Recon
SK-Disbur
I was expecting the other way around with Disbur before Recon, since hyphen comes before underscore.
Here is my code:
List<string> list=new List<string>();
list.Add("SK-Disbur");
list.Add("SK_Recon");
list.Sort();
for(int i=0;i <list.Count;i ++)
{
Console.WriteLine(list[i]);
}
.NET doesn't use ASCII, it uses Unicode UTF-16. When you perform a string sort, By default .NET are using the current culture's rules for sorting. In this case, those rules indicate that "_" comes before "-". You can get the result you expect by using the "ordinal" string comparer:
List<string> list = new List<string>()
{
"SK-Disbur",
"SK_Recon",
"SK1",
"SK2"
};
list.Sort(StringComparer.Ordinal);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
you will get this result
SK-Disbur
SK1
SK2
SK_Recon
but if you use list.Sort() (by default) the result is very different
SK_Recon
SK-Disbur
SK1
SK2
By default list.Sort() uses the current culture's rules for sorting. That means each character is sorted using a linguistic sort (alphabetic sequence). You should use ordinal sorting so that its sorted by the binary value of each character. Try:
list.Sort(StringComparer.Ordinal);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I've been looking up a lot of videos, tried using the bubble sort method, insert sort method, but nothing seems to work for this particular problem. I am supposed to add a string (movie name) to an array, but I must do it alphabetically. I can not sort the array after it's completed, it must be done while I add the new strings.
I've seen a lot posts with similar questions like this but all of them sort the array after its completed!
Here is a couple of methods that might help you.
private void PrintAlphabetically()
{
string[] movies = new string[5];
movies[0] = "b";
movies[1] = "x";
movies[2] = "m";
movies[3] = "a";
movies[4] = "t";
AddToStringArray(ref movies, "s");
Array.Sort(movies, (x, y) => String.Compare(x , y));
for (int i = 0; i < movies.Length; i++)
{
Console.WriteLine(movies[i]);
}
}
private void AddToStringArray(ref string[] array, string item)
{
List<string> list = array.OfType<string>().ToList();
list.Add(item);
array = list.ToArray();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How can I find equivalent(Not equal) values between strings?
//Equivalent Values
string A = "Beneficiation Return";
string B = "Return Beneficiation";
string C = "Beneficiation From Return";
string D = "Return From Beneficiation";
if i use
if(A == B)//Equal
It will only compare Equal strings and they are equivalent but not equal, is there any way to verify equivalence?
Equivalence can be: shuffled words, have or not linking words(Just
five:For,To,In,From,At.) or be shuffled and with linking words
the code would results in:
("Beneficiation Return" == "Return Beneficiation")True
("Beneficiation From Return" == "Return Beneficiation")True
("Return Beneficiation" == "Return From Beneficiation")True
I think this is what you're looking for..
string a = "Beneficiation Return";
string b = "Return Beneficiation";
string c = "Beneficiation From Return";
string d = "Return From Beneficiation";
bool isSame = !a.Except(b).Any() && !b.Except(a).Any();
The bool isSame will return true because strings a & b contain the same characters.
Compare a with c and it will return false
You could create a helper function: equivalentStrings that accepts both A and B. In that function you could do the following:
Split both strings A and B, and get two arrays of words
Check if they have same count of words (return false if not)
Check each word in arrayA if there is same word in arrayB, if yes remove the word from both arrays if not return false
If arrayA is empty (all words removed) return true.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm very new to c#. I created a forms. A richtextbox and a button in it.
I have a list of operators: Sum, Subtract,Multi,Div.I want to run a small richtextbox test. For example,in the richtextbox I write a text (eg. Sum(1,2)) and then click the button. A return result(eg.3) prints in the richtextbox.
My idea is to use string contains
foreach( var element in operatorlist)
{
string text=richtextbox.text;
if( text.contains(element)== true)
{
element(parameter1,parameter2);//something like this
}
}
I met two questions right row.
My first question is how to get the mathematical operation from the richtextbox text. Is there a better way than mine?
My second question is once we know the operator,how to allocate the two parameters in the richtextbox to the operator.
I'm not asking for coding, I'm just looking for ideas. If you have a good idea and wish to share.
You can evaluate an expression using the DataTable.Compute function:
int p1 = 1 ; string s1 = p1.ToString() ;
int p2 = 2 ; string s2 = p2.ToString() ;
int p3 = 3 ; string s3 = p3.ToString() ;
// Compute (p1+p2)*p3 ==> 9
int result = new DataTable().Compute( "("+s1+"+"+s2+")*"+s3+")","") ;
or directly:
string expression = "(1+2)*3" ;
int result = new DataTable().Compute(expression,"") ;
I think this comes down to personal style. Your way will definitely work, so good on you for that. The way I would do it is to create a Dictionary of strings to an enum. So for example, said dictionary and enum might look like this:
enum Operator { Addition = 0, Subtraction = 1, Multiplication = 2, Division = 3, etc};
var operatorDictionary = new Dictionary<string, Operator>()
{
{"Addition", Operator.Addition},
{"Subtraction", Operator.Subtraction},
etc...
};
Then to get the value you would just do
Operator operation;
operatorDictionary.TryGetValue(string operationString, out operation);
and you would have to then build some code that switches through the Operators and performs the correct operation. There is even a way of converting a string to an enum, so that would work as well.
It looks like the parameters are in a consistent format, so you would just make a simple method that splits by parenthesis and the comma, and returns the strings that it found.
Let me know if you need anything explained more.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The question is on C#.
I have a string "value1=1234;value2=2345;value3=3456;value4= ..."
What is the best way to retrieve the values?
I thought about String.Split(";") but I don't know how to retrieve the values only. The result I get includes the prefix I don't want.
I only want the values of "1234", "2345", "3456"... nothing else, and them put them into a list of strings.
How do I solve this? Thanks.
If the format is always fixed, you can do it fairly easily via LINQ:
List<string> values = theString.Split(';').Select(s => s.Split('=')[1]).ToList();
Note that you may want to use RemoveEmptyEntries if your input string ends in a semi-colon:
List<string> values = theString
.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Split('=')[1]).ToList();
This would prevent an exception from occuring within the Select. If the input doesn't end in a semi-colon, however, this wouldn't be necessary.
var text = "value1=1234;value2=2345;value3=3456;value4= ...";
var pieces = text.Split('=');
var values = new Dictionary<string,string>();
for(int index = 0; index < pieces.Length; index += 2)
{
values.Add(pieces[index], pieces[index + 1]);
}
This will give you a dictionary of the pairs where the key is the left-hand side of the '=' and the value is the string representation of the value, which allows your to do:
var value1 = values["value1"];
var value2 = values["value2"];