string-comparison

Comparing a string with the empty string (Java)

偶尔善良 提交于 2019-11-26 15:32:25
问题 I have a question about comparing a string with the empty string in Java. Is there a difference, if I compare a string with the empty string with == or equals ? For example: String s1 = "hi"; if (s1 == "") or if (s1.equals("")) I know that one should compare strings (and objects in general) with equals , and not == , but I am wondering whether it matters for the empty string. 回答1: s1 == "" is not reliable as it tests reference equality not object equality (and String isn't strictly canonical)

What are some algorithms for comparing how similar two strings are?

故事扮演 提交于 2019-11-26 15:09:25
问题 I need to compare strings to decide whether they represent the same thing. This relates to case titles entered by humans where abbreviations and other small details may differ. For example, consider the following two titles: std::string first = "Henry C. Harper v. The Law Offices of Huey & Luey, LLP"; As opposed to: std::string second = "Harper v. The Law Offices of Huey & Luey, LLP"; A human can quickly gauge that these are most likely one and the same. The current approach I have taken is

how to compare list elements(type string) and string(in request scope) using struts 2 tags

安稳与你 提交于 2019-11-26 14:49:32
问题 My List contains("A","B","C","D") elements <s:iterator value="lis"> <s:property /><br> </s:iterator> and String str="A"; <s:property value="%{#request.str}"/> I want to compare every element of list(lis) with String s. 回答1: With the IteratorStatus object: <s:iterator value="lis" status="ctr"> <s:property /> <s:if test="%{#request.str.equals(lis[#ctr.index])}"> -> This value from "lis" is equal to the value of "str" </s:if> <br/> </s:iterator> With the var parameter: <s:iterator value="lis"

Case insensitive string comparison C++ [duplicate]

為{幸葍}努か 提交于 2019-11-26 14:37:37
问题 This question already has an answer here: Case-insensitive string comparison in C++ [closed] 31 answers I know there are ways to do case ignore comparison that involve iterating through strings or one good one on SO needs another library. I need to put this on other computers that might not have it installed. Is there a way to use the standard libraries to do this? Right now I am just doing... if (foo == "Bar" || foo == "bar") { cout << "foo is bar" << endl; } else if (foo == "Stack Overflow"

MySQL query String contains

Deadly 提交于 2019-11-26 14:14:40
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 Quite simple actually: mysql_query(" SELECT * FROM `table` WHERE `column` LIKE '%{$needle}%' "); The % is a wildcard for any character. Do note that this can get slow on very large datasets so if your database grows you'll need to

String comparison and String interning in Java

北城余情 提交于 2019-11-26 13:47:38
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! 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 some other reason) You know you're dealing with compile-time string constants You've manually interned the

String Comparison in Java

﹥>﹥吖頭↗ 提交于 2019-11-26 12:50:47
What does "compare two strings lexicographically" mean? 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 first (comes first in a dictionary) returns == 0 then the two strings are lexicographically equivalent returns

Checking whether a string starts with XXXX

丶灬走出姿态 提交于 2019-11-26 11:40:29
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? RanRag aString = "hello world" aString.startswith("hello") More info about startwith Shawabawa 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 re.match(r'^hello', somestring): # do stuff Obviously, in this case, somestring.startswith('hello')

Similarity scores based on string comparison in R (edit distance)

对着背影说爱祢 提交于 2019-11-26 09:23:50
问题 I am trying to assign similarity score based on comparison between 2 strings. Is there a function for the same in R. I am aware of such a function in SAS by the name of SPEDIS. Please let me know if there is such a function in R. 回答1: The function adist computes the Levenshtein edit distance between two strings. This can be transformed into a similarity metric as 1 - (Levenshtein edit distance / longer string length). The levenshteinSim function in the RecordLinkage package also does this

How to compare Unicode characters that “look alike”?

做~自己de王妃 提交于 2019-11-26 07:57:03
问题 I fall into a surprising issue. I loaded a text file in my application and I have some logic which compares the value having µ. And I realized that even if the texts are same the compare value is false. Console.WriteLine(\"μ\".Equals(\"µ\")); // returns false Console.WriteLine(\"µ\".Equals(\"µ\")); // return true In later line the character µ is copy pasted. However, these might not be the only characters that are like this. Is there any way in C# to compare the characters which look the same