c# compare strings in different language

2.4k Views Asked by At

I have some strings in different languages; for example:

  • "kot jest dobry" (in Polish)
  • "cat is good" (in English)

They are captions of buttons that are translated from English to the language chosen from the user.

Which is the best way to compare these captions, programmatically?

2

There are 2 best solutions below

1
On BEST ANSWER

Since both the strings are translations of one another you can maintain a translation table and if you want to find out if two strings are same you can just look them up in your table and if they happen to fall in the same row, then they are equal for example

class TranslatedText
{
      public int Id {get; set; }
      public string Language {get; set; }
      public string Text {get; set; }
}

So populate a list of TranslatedText objects with each string and assign same id to same pieces of text. Later to compare you can lookup the corresponding object in list and check the Id like so

var first = translatedTextList.FirstOrDefault(t=>t.Text.Equals(firstString));
var second = translatedTextList.FirstOrDefault(t=>t.Text.Equals(secondString));
bool areSame = (first != null & second !=null & first.Id == second.Id);
return areSame;

Assuming the strings you want to compare are called firstString and secondString;

1
On

override .equal() function or convert it to char array then compare!

here you find HOW:

http://msdn.microsoft.com/en-us/library/ms173147.aspx