string-comparison

locale aware string comparision

笑着哭i 提交于 2019-12-22 15:38:14
问题 I´m using strcmp in combination with usort in order to sort an array of country names. Currently, the sort order is: Belgien Frankreich Italien Luxemburg Niederlande Spanien United Kingdom Österreich Which is correct, apart from the position of Österreich . It should be between Niederlande and Spanien . I also tried strnatcmp and strcoll (with setlocale ), but the sort order was not the way I wanted it. The results are not from a mysql db, so sorting via a mysql query is not an option. 回答1:

I'm implementing a CaseAccentInsensitiveEqualityComparer for Strings. I'm not sure how to implement the GetHashCode

女生的网名这么多〃 提交于 2019-12-22 09:45:10
问题 My code is like this: public class CaseAccentInsensitiveEqualityComparer : IEqualityComparer<string> { public bool Equals(string x, string y) { return string.Compare(x, y, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) == 0; } public int GetHashCode(string obj) { // not sure what to put here } } I know the role of GetHashCode in this context, what I'm missing is how to produce the InvariantCulture , IgnoreNonSpace and IgnoreCase version of obj so that

R grepl: quickly match multiple strings against multiple substrings, returning all matches

被刻印的时光 ゝ 提交于 2019-12-22 09:29:56
问题 I have a fairly large set of strings in R: set.seed(42) strings <- sapply(1:250000, function(x) sample(2:20, 1, prob=c( 0.001, 0.006, 0.021, 0.043, 0.075, 0.101, 0.127, 0.138, 0.132, 0.111, 0.087, 0.064, 0.042, 0.025, 0.014, 0.008, 0.004, 0.002, 0.001))) strings <- lapply(strings, function(x) sample(letters, x, replace=TRUE)) strings <- sapply(strings, paste, collapse='') I would like to make a list denoting the presence or absence of each element from a list of substrings within these

Why should I use string.length == 0 over string == “” when checking for empty string in ECMAScript?

旧街凉风 提交于 2019-12-22 03:07:22
问题 Most of the developers on my current project use a (to me) strange way to check for empty strings in ECMAScript: if (theString.length == 0) // string is empty I would normally write this instead: if (theString == "") // string is empty The latter version seems more readable and natural to me. Nobody I asked seemed to be able to explain the advantages of version 1. I guess that at some time in the past somebody told everybody that this is the way to do it, but now that person left and nobody

In R - fastest way pairwise comparing character strings on similarity

青春壹個敷衍的年華 提交于 2019-12-22 01:34:40
问题 I'm looking for a way to speed up the following approach. Any pointers are very welcome. Where are the bottlenecks? Say I have the following data.frame : df <- data.frame(names=c("A ADAM", "S BEAN", "A APPLE", "J BOND", "J BOND"), v1=c("Test_a", "Test_b", "Test_a", "Test_b", "Test_b"), v2=c("Test_c", "Test_c", "Test_d", "Test_d", "Test_d")) I want to compare each pair of rows in df on their JaroWinkler similarity. With some help of others (see this post), I've been able to construct this code

Optimized strcmp implementation

谁说我不能喝 提交于 2019-12-21 03:47:13
问题 This function was found here. It's an implementation of strcmp : int strcmp(const char* s1, const char* s2) { while (*s1 && (*s1 == *s2)) s1++, s2++; return *(const unsigned char*)s1 - *(const unsigned char*)s2; } I understand all but the last line, in short what is going on in the last line? 回答1: return *(const unsigned char*)s1-*(const unsigned char*)s2; OP: in short what is going on in the last line? A: The first potential string difference is compared. Both chars are referenced as

comparing two strings in SQL Server

…衆ロ難τιáo~ 提交于 2019-12-21 03:37:21
问题 Is there any way to compare two strings in SQL Server 2008 stored procedure like below? int returnval = STRCMP(str1, str2) returns 0 if the strings are the same returns -1 if the first argument is smaller than the second according to the current sort order. returns 1 otherwise. Above method I find in the MySQL but not in SQL Server. 回答1: There is no direct string compare function in SQL Server CASE WHEN str1 = str2 THEN 0 WHEN str1 < str2 THEN -1 WHEN str1 > str2 THEN 1 ELSE NULL --one of the

Is there any way to sort strings in all languages?

自古美人都是妖i 提交于 2019-12-20 12:22:04
问题 I have this code. It sorts correctly in French and Russian. I used Locale.US and it seems to be right. Is this solution do right with all languages out there? Does it work with other languages? For example: Chinese, Korean, Japanese... If not, what is the better solution? public class CollationTest { public static void main(final String[] args) { final Collator collator = Collator.getInstance(Locale.US); final SortedSet<String> set = new TreeSet<String>(collator); set.add("abîmer"); set.add(

How to find a similar word for a misspelled one in PHP?

大城市里の小女人 提交于 2019-12-20 09:01:47
问题 I'll explain my problem: I have a database table called country . It has two columns: ID and name . When I want to search for 'paris' , but misspelled the word: 'pares' ( 'e' instead of 'i' ), I won't get any result from DB. I want the the system to suggest similar words that could help in the search. So, I am looking for help writing a script that makes suggestions from the DB that contain similar words like: paris, paredes, ... etc. 回答1: In PHP you should use metaphone it is more accurate

Date comparison in PHP

狂风中的少年 提交于 2019-12-20 04:30:32
问题 I currently have a date that's being stored in my SQL database as a VARCHAR of 255 characters. I declared this string as //within an object... $date = date(DATE_RFC822); Now, later on in the coding, I realise that I need to actually compare dates with each other. My initial, very naive attempt looked a little bit like this: if(object_1->date > object_2->date){ //do this that assumes that object_1 was created at a later date than object_2 }else{ continue; } While this worked fine for different