string-comparison

strcmp or string::compare?

孤人 提交于 2019-11-28 09:16:20
I want to compare two strings. Is it possible with strcmp ? (I tried and it does not seem to work). Is string::compare a solution? Other than this, is there a way to compare a string to a char ? Thanks for the early comments. I was coding in C++ and yes it was std::string like some of you mentioned. I didn't post the code because I wanted to learn the general knowledge and it is a pretty long code, so it was irrelevant for the question. I think I learned the difference between C++ and C, thanks for pointing that out. And I will try to use overloaded operators now. And by the way string:

Java: String: equalsIgnoreCase vs switching everything to Upper/Lower Case

荒凉一梦 提交于 2019-11-28 08:08:27
It came to my attention that there a several ways to compare strings in Java. I just got in the habit ages ago to use equalsIgnoreCase to avoid having problems with case sensitive strings. Others on the other hand prefer passing everything in upper or lower case. From where I stand (even if technically I'm sitting), I don't see a real difference. Does anybody know if one practice is better than the other? And if so why? Use equalsIgnoreCase because it's more readable than converting both Strings to uppercase before a comparison. Readability trumps micro-optimization . What's more readable? if

strcmp() return different values for same string comparisons

一曲冷凌霜 提交于 2019-11-28 07:30:23
问题 char s1[] = "0"; char s2[] = "9"; printf("%d\n", strcmp(s1, s2)); // Prints -9 printf("%d\n", strcmp("0", "9")); // Prints -1 Why do strcmp returns different values when it receives the same parameters ? Those values are still legal since strcmp's man page says that the return value of strcmp can be less, greater or equal than 0, but I don't understand why they are different in this example. 回答1: I assume you are using GCC when compiling this, I tried it on 4.8.4. The trick here is that GCC

Compare two NSStrings

会有一股神秘感。 提交于 2019-11-28 07:30:14
In my app there is a mechanism that requires that at a certain point two NSString s will be the same to do something; for some reason when I compare the two, even when they are the same, it still doesn't recognize that. The code is something like this: NSString * aString = [self someMethodThatGetsAString]; NSString * bString; BOOL areStringsTheSame = NO; while (areStringsTheSame != YES) { bString = [self someMethodThatTakesNSStringsFromAnArrey]; if (bString == aString) { areStringsTheSame = YES; { } I even inserted an NSLog() and made sure that at a certain point they were the same (and as far

equal() and equalsIgnoreCase() return false for equal strings

六眼飞鱼酱① 提交于 2019-11-28 07:06:53
问题 I'm working with eclipse IDE (Version: 3.4.2) on a mac and I have met the following issue. When comparing between strings using equal() or equalsIgnoreCase() methods I receive false even when the string are equal. For example, the code below consider the following condition as false, even when values[0] = "debug_mode" if (values[0].equalsIgnoreCase("debug_mode")) debug_mode = true; which is part of the following loop: String value = dis.readLine(); String values[] = value.trim().split("=");

Complex “Contains” string comparison

半城伤御伤魂 提交于 2019-11-28 06:57:22
I'm developing a C# 4.5 app and I need a function to return true for the following comparison: "bla LéOnAr d/o bla".ComplexContains("leonardo") In other words, I need string.Compare(str1, str2, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreNonSpace) to also check for "contains! Can anyone help? You could use an appropriate CompareInfo and then CompareInfo.IndexOf(string, string, CompareOptions) and check the result against -1. Sample: using System; using System.Globalization; class Program { static void Main() { var compareInfo =

If statement with String comparison fails [duplicate]

試著忘記壹切 提交于 2019-11-28 05:42:18
This question already has an answer here: How do I compare strings in Java? 23 answers I really don't know why the if statement below is not executing: if (s == "/quit") { System.out.println("quitted"); } Below is the whole class. It is probably a really stupid logic problem but I have been pulling my hair out over here not being able to figure this out. Thanks for looking :) class TextParser extends Thread { public void run() { while (true) { for(int i = 0; i < connectionList.size(); i++) { try { System.out.println("reading " + i); Connection c = connectionList.elementAt(i); Thread.sleep(200)

What is difference between different string compare methods [duplicate]

浪尽此生 提交于 2019-11-28 05:17:28
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) Ripped from msdn string.Equals Determines whether this instance and a specified object, which must also be a String object, have the same value. string.Compare Compares two specified String objects and returns an integer that indicates their relative position in the sort order. string

Is it safe to compare strings with 'greater than' and 'less than' in MySQL?

南笙酒味 提交于 2019-11-28 03:37:06
问题 MySQL (5.1.41-3ubuntu12.10-log) seems to give predictable results on string comparison using > (greater than) and < (less than): select "a" > "a", "a" > "b", "b" > "a", "ab" > "aa", "ab" > "aabbbb"; +-----------+-----------+-----------+-------------+-----------------+ | "a" > "a" | "a" > "b" | "b" > "a" | "ab" > "aa" | "ab" > "aabbbb" | +-----------+-----------+-----------+-------------+-----------------+ | 0 | 0 | 1 | 1 | 1 | +-----------+-----------+-----------+-------------+---------------

Bash compare output rather than command

自作多情 提交于 2019-11-27 23:55:36
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 compare the outputs. What am I doing wrong? Thanks Try; if [ "$LOCALMD5" == "$REMOTEMD5" ] which should