string-comparison

If “a == b” is false when comparing two NSString objects

匆匆过客 提交于 2019-11-26 07:49:51
问题 I have a class with an accessible method that passes back an NSString when called. [MyClass getMyString] The string variable in that class is actually assigned in the didSelectRowAtIndexPath: part of a table like this: myString = cell.textLabel.text; When I retrieve the string by calling that method, I assign it to another string in the class that called it and compare it to a string I have defined NSString *mySecondString; mySecondString = @\"my value\"; if(mySecondString == myString){ i = 9

Comparing a string to multiple items in Python

女生的网名这么多〃 提交于 2019-11-26 06:45:59
问题 I\'m trying to compare a string called facility to multiple possible strings to test if it is valid. The valid strings are: auth, authpriv, daemon, cron, ftp, lpr, kern, mail, news, syslog, user, uucp, local0, ... , local7 Is there an efficient way of doing this other than: if facility == \"auth\" or facility == \"authpriv\" ... 回答1: If, OTOH, your list of strings is indeed hideously long, use a set: accepted_strings = {'auth', 'authpriv', 'daemon'} if facility in accepted_strings: do_stuff()

How do I compare two strings in Perl?

假如想象 提交于 2019-11-26 06:31:15
问题 How do I compare two strings in Perl? I am learning Perl, I had this basic question looked it up here on StackOverflow and found no good answer so I thought I would ask. 回答1: See perldoc perlop. Use lt , gt , eq , ne , and cmp as appropriate for string comparisons: Binary eq returns true if the left argument is stringwise equal to the right argument. Binary ne returns true if the left argument is stringwise not equal to the right argument. Binary cmp returns -1, 0, or 1 depending on whether

MySQL query String contains

最后都变了- 提交于 2019-11-26 03:49:14
问题 I\'ve been trying to figure out how I can make a query with MySQL that checks if the value (string $haystack ) in a certain column contains certain data (string $needle ), like this: mysql_query(\" SELECT * FROM `table` WHERE `column`.contains(\'{$needle}\') \"); In PHP, the function is called substr($haystack, $needle) , so maybe: WHERE substr(`column`, \'{$needle}\')=1 回答1: Quite simple actually: mysql_query(" SELECT * FROM `table` WHERE `column` LIKE '%{$needle}%' "); The % is a wildcard

String comparison and String interning in Java

你说的曾经没有我的故事 提交于 2019-11-26 03:44:42
问题 When should one compare String s as objects and when should one use their equals method? To make sure, I always use equals , but that doesn\'t seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use? Thanks! 回答1: You should almost always use equals . You can be certain that string1 == string2 will work if: You've already made sure you've got distinct values in some other way (e.g. you're using string values fetched from a set, but comparing them for

String Comparison in Java

☆樱花仙子☆ 提交于 2019-11-26 03:35:38
问题 What does \"compare two strings lexicographically\" mean? 回答1: Leading from answers from @Bozho and @aioobe, lexicographic comparisons are similar to the ordering that one might find in a dictionary. The Java String class provides the .compareTo () method in order to lexicographically compare Strings. It is used like this "apple".compareTo ("banana") . The return of this method is an int which can be interpreted as follows: returns < 0 then the String calling the method is lexicographically

Case-insensitive search

不羁的心 提交于 2019-11-26 03:03:42
问题 I\'m trying to get a case-insensitive search with two strings in JavaScript working. Normally it would be like this: var string=\"Stackoverflow is the BEST\"; var result= string.search(/best/i); alert(result); The /i flag would be for case-insensitive. But I need to search for a second string; without the flag it works perfect: var string=\"Stackoverflow is the BEST\"; var searchstring=\"best\"; var result= string.search(searchstring); alert(result); If I add the /i flag to the above example

Checking whether a string starts with XXXX

徘徊边缘 提交于 2019-11-26 02:18:24
问题 I would like to know how to check whether a string starts with \"hello\" in Python. In Bash I usually do: if [[ \"$string\" =~ ^hello ]]; then do something here fi How do I achieve the same in Python? 回答1: aString = "hello world" aString.startswith("hello") More info about startwith 回答2: RanRag has already answered it for your specific question. However, more generally, what you are doing with if [[ "$string" =~ ^hello ]] is a regex match. To do the same in Python, you would do: import re if

Difference between InvariantCulture and Ordinal string comparison

送分小仙女□ 提交于 2019-11-26 01:50:31
问题 When comparing two strings in c# for equality, what is the difference between InvariantCulture and Ordinal comparison? 回答1: InvariantCulture Uses a "standard" set of character orderings (a,b,c, ... etc.). This is in contrast to some specific locales, which may sort characters in different orders ('a-with-acute' may be before or after 'a', depending on the locale, and so on). Ordinal On the other hand, looks purely at the values of the raw byte(s) that represent the character. There's a great

How can I do a case insensitive string comparison?

南楼画角 提交于 2019-11-26 00:59:14
问题 How can I make the line below case insensitive? drUser[\"Enrolled\"] = (enrolledUsers.FindIndex(x => x.Username == (string)drUser[\"Username\"]) != -1); I was given some advice earlier today that suggested I use: x.Username.Equals((string)drUser[\"Username\"], StringComparison.OrdinalIgnoreCase))); the trouble is I can\'t get this to work, I\'ve tried the line below, this compiles but returns the wrong results, it returns enrolled users as unenrolled and unenrolled users as enrolled. drUser[\