This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ignoring accented letters in string comparison
I am using a special framework for making this job (NHibernate, Castle). My problem is that; there is a website where people can search apartments, rooms etc. Most of these website users are Turkish. So my problem starts here.
For example if people search the word: Beşiktaş(it is a district name) they can search like this Besiktas. As you can see there are some special characters(S,Ş Ö O, Ğ G, Ç C, İ I, Ü U ) in Turkish and people might be using both of them. I have to search all conditions like this. For example if they try to search Beşiktaş i have to search all variants like:"Besiktaş, Beşiktaş, Besiktas Beşiktas" and after this operation I have to remove duplicated objects from my list. How can I make this dream come true :)
I Just need the algorithm of this operation.
Sorry for my poor English skills. Thank You
You need to look into something called CultureInfo which can be leveraged to do this. Here is a similar question someone else asked regarding ignoring accented letters, which is pretty much the same as what you wish to do. Here is a method that was posted and accepted as an answer which will compare strings ignoring accented characters
string s1 = "hello";
string s2 = "héllo";
if (String.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == 0)
{
// both strings are equal
}
If you use this I would store the districts as their English names without accented letters and compare the string before you execute your query against your database. That way you would allow the user to use accented characters as well as their English names.
Here is a link to the MSDN article regarding CultureInfo
Related
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 6 years ago.
Improve this question
I need some help with some operators in the If statement...
I am running a code like this
if (statement.Contains("weather") &&
(
statement.Contains("what") || statement.Contains("how")
)
)
{
statement = ("It's " + Weather.Get_Weather("condition") + " outside");
}
but it's not working... can any one help me with this, because I cannot find any mistake in this code and I don't even have that much experience with this type of operators like || and && because I can use If Statements inside If Statements.
And I want the statement to have Weather and What or How so I can confirm It's a Question, Or the user is asking for the weather...
You can Also gimme more ideas on this...
'how is the weather' this is not going through the If statement
it contains How and weather that's enough...
I am Really Sorry for my Question, It was My Problem I did not know that Contains() is Case-Sensitive...
Assuming the statement you're testing is actually 'How is the weather' then your if statement is working as expected. Your checks are seeing if the statement contains 'weather' and 'what' OR contains the word 'how' (note the lower case).
As your phrase doesn't contain the word 'what' the first check (for the words 'weather' AND 'what') will be false. Also, as the word 'How' starts with a capital 'H' it won't match against 'how', so will also return false and therefore not enter the if statement.
If you want your search to be case insensitive then you will need to consider the language as well, as words all in upper case in some languages mean different things to the same word in all lower case. Here's a similar question and answer, accepted answer detailed below for completeness:
To test if the string paragraph contains the string word (thanks
#QuarterMeister) culture.CompareInfo.IndexOf(paragraph, word,
CompareOptions.IgnoreCase) >= 0
Where culture is the instance of CultureInfo describing the language
that the text is written in.
This solution is transparent about the definition of
case-insensitivity, which is language dependent. For example, the
English language uses the characters I and i for the upper and lower
case versions of the ninth letter, whereas the Turkish language uses
these characters for the eleventh and twelfth letters of its 29
letter-long alphabet. The Turkish upper case version of 'i' is the
unfamiliar character 'İ'.
Thus the strings tin and TIN are the same word in English, but
different words in Turkish. As I understand, one means 'spirit' and
the other is an onomatopoeia word. (Turks, please correct me if I'm
wrong, or suggest a better example)
To summarise, you can only answer the question 'are these two strings
the same but in different cases' if you know what language the text is
in. If you don't know, you'll have to take a punt. Given English's
hegemony in software, you should probably resort to
CultureInfo.InvariantCulture, because it'll be wrong in familiar ways.
This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
I have following requirement in my application. I need to maintain password complexity in my application as mention below.
password length is minimum 8 characters, a capital letter, a special character apart from # and numeric values which are not in sequence
Can anyone help me in getting regular expression for the above criteira or else C# code is also helpful for me.
Let me suggest a different approach: Instead of creating a fancy regular expression that confuses everyone reading it (including you, since you cannot come up with one on your own), just encode the logic in a few simple C# statements:
if (mypassword.Length < 8)
myerror = "The password must have a minimum length of 8 characters.";
else if (!Regex.IsMatch(mypassword, "[A-Z]"))
myerror = "The password must contain at least one capital letter.";
...
This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 9 years ago.
My question was marked as a duplicate so I've made a couple edits. As I said, I was able to find many similar questions when I searched but none were quite what I needed. I am not validating a string where the only thing present will be the phone number (this seems to be what most of the other questions are addressing). Rather, I am attempting to pull out all phone numbers (which will then be manually checked by the user) from a larger block of text. The problem I am having is that my regular expression is matching zip codes with extensions (ex: 45202-4787), and I am not sure how to alter my regex to avoid that. If this truly is a duplicate question then I apologize for not being able to find the existing one that deals with my issue.
My specifications for phone number format are:
1) -, ., and space as delimiters (and in any combination)
2) area code may appear with or without parentheses
A few examples:
(xxx) xxx-xxxx
(xxx) xxx.xxxx
xxx-xxx-xxxx
xxx xxx-xxxx
xxxxxxxxxxx
I am using Anirudh's regex from the comments:
(\(?\d{3}\)?)?[. -]?\d{3}[. -]?\d{4}
Again, my problem is that this regex matches zip codes with extensions (ex: 45202-4787).
I would be grateful for any help, as I'm very new to using regular expressions. Thanks!
This should do it:
^(\([0-9]{3}\)|[0-9]{3})[ -\.]?[0-9]{3}[ -\.]?[0-9]{4}$
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to split a string while preserving line endings?
How do I split a string by strings and include the delimiters using .NET?
I'm splitting text into sentences. mystring.Split('.','!', '?') returns the sentences without the ./!/? on them. I need to have it return a sentence with the split param on the end? How does that go? Thanks
public static string[] GetSentences(string text)
{
return text.Split('.', '!', '?');
}
I can think of one way to do it, by combining two separate arrays, but I think it looks awful so I thought I'd ask you professionals for a "proper" way :D
Edit - never mind close its a duplicate. I found the other threads, sorry
Right, string.Split() isn't the right tool here.
Either simply loop through it (string.IndexOf())
or use a RegEx: ([^\.!?]+[\.!?])*
I'm not 100% sure about the escaping.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Does anyone know a good .NET dictionary API? I'm not interested in meanings, rather I need to be able to query words in a number of different ways - return words of x length, return partial matches and so on...
Grab the flat text file from an open source spellchecker like ASpell (http://aspell.net/) and load it into a List or whatever structure you like.
for example,
List<string> words = System.IO.File.ReadAllText("MyWords.txt").Split(new string[]{Environment.NewLine}).ToList();
// C# 3.0 (LINQ) example:
// get all words of length 5:
from word in words where word.length==5 select word
// get partial matches on "foo"
from word in words where word.Contains("foo") select word
// C# 2.0 example:
// get all words of length 5:
words.FindAll(delegate(string s) { return s.Length == 5; });
// get partial matches on "foo"
words.FindAll(delegate(string s) { return s.Contains("foo"); });
You might want to look for a Trie implementation. That will certainly help with "words starting with XYZ" as well as exact matches. You may well want to have all of your data in multiple data structures, each one tuned for the particular task - e.g. one for anagrams, one for "by length" etc. Natural language dictionaries are relatively small compared with RAM these days, so if you really want speedy lookup, that's probably the way to go.
Depending on how involved your queries are going to be, it might be worth investigating WordNet, which is basically a semantic dictionary. It includes parts of speech, synonyms, and other types of relationships between the words.
NetSpell (http://www.loresoft.com/netspell/) is a spell checker that's written in .NET that has word listings in several languages that you could use.
I'm with Barry Fandango on this one, but you can do it without LINQ. .NET 2.0 has some nice filtering methods on the List(T) type. The one I suggest is
List(T).FindAll(Predicate(T)) : List(T)
This method will put every element in the list through the predicate method and return the list of words that return 'true'. So, load your words as suggested from an open source dictionary into a List(String). To find all words of length 5...
List(String) words = LoadFromDictionary();
List(String) fiveLetterWords = words.FindAll(delegate(String word)
{
return word.Length == 5;
});
Or for all words starting with 'abc'...
List(String) words = LoadFromDictionary();
List(String) abcWords = words.FindAll(delegate(String word)
{
return word.StartsWith('abc');
});