string-comparison

Check if one string is a prefix of another

末鹿安然 提交于 2019-11-27 00:57:07
问题 I have two strings which I'd like to compare: String and String: . Is there a library function that would return true when passed these two strings, but false for say String and OtherString ? To be precise, I want to know whether one string is a prefix of another. 回答1: Use std::mismatch. Pass in the shorter string as the first iterator range and the longer as the second iterator range. The return is a pair of iterators, the first is the iterator in the first range and the second, in the

What is difference between different string compare methods [duplicate]

ぃ、小莉子 提交于 2019-11-27 00:52:00
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Differences in string compare methods in C# In .NET there are many string comparison methods, I just want to confirm which one is the best to use considering performance. string.Equals() string.Compare() string.CompareTo() string.CompareOrdinal() string.ReferenceEquals() if (str1 == str2) 回答1: Ripped from msdn string.Equals Determines whether this instance and a specified object, which must also be a String

C++ Compare char array with string

梦想与她 提交于 2019-11-26 22:16:59
问题 I'm trying to compare a character array against a string like so: const char *var1 = " "; var1 = getenv("myEnvVar"); if(var1 == "dev") { // do stuff } This if statement never validates as true... when I output var1 it is "dev", I was thinking maybe it has something to do with a null terminated string, but the strlen of "dev" and var1 are equal... I also thought maybe var1 == "dev" was comparing "dev" against the memory location of var1 instead of the value. *var1 == "dev" results in an error.

C#: Confusion about ToUpper() and ToLower()

天涯浪子 提交于 2019-11-26 21:47:56
问题 if I do something like this... String myVar = "in"; if(myVar.ToUpper() == "in") { //do something } This is not going to go inside "if" block ..right? or Is it going to check BOTH for "in" AND "IN" and do whatever is there inside that if? If so, why is that ? Isn't it supposed to skip what's inside of "if" block? Same confusion is about ToLower() too Edit : So to check for both cases, I need to write: if((myVar.ToUpper().Equals("in"))&&(myVar.Equals("in"))) Like this..right? 回答1: Rather than

Bash compare output rather than command

荒凉一梦 提交于 2019-11-26 21:35:25
问题 Trying to create a script to read a remote file and check the md5 checksum and alert if a mismatch yet getting an error I can't understand. #!/bin/sh REMOTEMD5=$(ssh user@host 'md5sum file.txt') LOCALMD5=$(md5sum 'file.txt') if [$LOCALMD5 !== $REMOTEMD5] then echo "all OK" else echo -e "no match, Local:"$LOCALMD5"\nRemote:"$REMOTEMD5 fi This returns line 4: [6135222a12f06b2dfce6a5c1b736891e: command not found I've tried using ' or " around the $LOCALMD5 but never seem able to get this to

Version number comparison in Python

随声附和 提交于 2019-11-26 21:31:22
I want to write a cmp -like function which compares two version numbers and returns -1 , 0 , or 1 based on their compared valuses. Return -1 if version A is older than version B Return 0 if version A and B are equivalent Return 1 if version A is newer than version B Each subsection is supposed to be interpreted as a number, therefore 1.10 > 1.1. Desired function outputs are mycmp('1.0', '1') == 0 mycmp('1.0.0', '1') == 0 mycmp('1', '1.0.0.1') == -1 mycmp('12.10', '11.0.0.0.0') == 1 ... And here is my implementation, open for improvement: def mycmp(version1, version2): parts1 = [int(x) for x in

How to compare Unicode characters that “look alike”?

一笑奈何 提交于 2019-11-26 21:29:08
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 but are actually different? BoltClock In many cases, you can normalize both of the Unicode characters to a

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

Deadly 提交于 2019-11-26 21:03:27
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; } I have stepped through the code and every time it evaluates the if statement, it skips right past the

Comparing a string to multiple items in Python

时光怂恿深爱的人放手 提交于 2019-11-26 21:01:39
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" ... pillmuncher 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() Testing for containment in a set is O(1) on average. Unless your list of strings gets hideously long

Python: Why does (“hello” is “hello”) evaluate as True? [duplicate]

放肆的年华 提交于 2019-11-26 20:19:11
This question already has an answer here: About the changing id of an immutable string 5 answers Why does "hello" is "hello" produce True in Python? I read the following here : If two string literals are equal, they have been put to same memory location. A string is an immutable entity. No harm can be done. So there is one and only one place in memory for every Python string? Sounds pretty strange. What's going on here? carl Python (like Java, C, C++, .NET) uses string pooling / interning. The interpreter realises that "hello" is the same as "hello", so it optimizes and uses the same location