String.Equals() not working as intended
When using LINQ to Entities, it will automatically convert it to LINQ to SQL. And if the database field you are doing a .Equals on does not have a collate of NOCASE (SQLite in my example) then it will always be case-sensitive. In otherwords, the database defines how to do the string comparison rather than code.
Java string comparison not working as intended
It is because you are not comparing 2 Strings
. You have to put it like this:
Log.e("testLogic", String.valueOf(taken.getText().toString().trim().equals("1")));
because .equals()
function needs two Strings
. Supposing that s1
and s2
are Strings
, you should do:
s1.equals(s2);
I expect it will be helpful for you!
String comparison logic not working as intended
Your logic is incorrect. It is never the case that it's equal to all 4 of the strings at the same time. You want the logical-OR ||
inside your parentheses:
if (!(meridian.toUpperCase().equals("AM") ||
meridian.toUpperCase().equals("A.M.") ||
meridian.toUpperCase().equals("PM") ||
meridian.toUpperCase().equals("P.M.")))
That way if it's equal to one of the expected cases, then the exception won't be thrown.
C++ String comparison not working as intended
getline(cin, userAnswer)
is keeping the \n
. You might consider trimming the string with something like the following
getline(cin, userAnswer);
userAnswer.erase(userAnswer.find_last_not_of("\n\r") + 1);
No guarantees that this is the answer, but I've run across this a few times and it's just been a trailing \n
or \r
.
string.equals function not working correctly c#
When comparing 2 string values inside a LINQ lamda expression, It does a case sensitive comparison by default. But if you do not wish to have this behaviour, you can use a different overload of Equals
method which takes a comparison type enum as the second argument, and you can pass the type of comparison you want (Case sensitive or Case Insensitive). For case insensitive comparisons you may pass StringComparison.CurrentCultureIgnoreCase
var resultsWithCaseIgnored = someEntityList.Where(i => i.Name
.Equals("scott",StringComparison.CurrentCultureIgnoreCase)).ToList();
But when you do a LINQ to SQL statement where it is going to generate a SQL statement and executes it, the generated SQL will be same for both the overloads.
The case sensitivity of where clauses in your SQL statements depends on the collation settings you have on your db server / your db / your specific column.
You can verify your current setting by executing a sql statement like
SELECT CONVERT (varchar, SERVERPROPERTY('collation'));
When i ran this in my sql server, the result i got was "SQL_Latin1_General_CP1_CI_AS"
. The CI
part indicates, it is case insensitive. That means when i do where UserName='scott'
and where UserName='SCOTT'
, both will give me same result.
If you want to do a case sensitive check, there are ways to update your collation to be one which is case sensitive. But updating your db is not always a safe thing ( think about the implications)
What you can do is, query your db table, get the results to your C# objects then do a where clause ( which will be case sensitive).
var users= db.Users.Where(i => i.Username.Equals(username)).ToList();
// do a case sensitive check on users
var user = users.FirstOrDefault(s=>s.Username.Equals(username));
if(user!=null)
{
// user has the User object
}
else
{
// no user matching our CASE SENSITIVE UserName check.
}
Java string.equals(string) not behaving as expected
After god knows how many hours copying and pasting from the debugger to a hex editor I have found the problem and a solution that works.
As suggested the problem was whitespaces but not in the way I or (I think) others suspected.
For some reason that I have failed to get to the bottom of, I am getting non-breaking whitespaces (0x00A0) in my strings instead of normal whitespaces (Ox0020). This appears to be happening more or less at random and I haven't found the section of code responsible yet.
The work around at the moment is to start my equals() method with:
speciesEng=speciesEng.replace((char)0x00a0,(char)0x0020);
other.speciesEng=other.speciesEng.replace((char)0x00a0,(char)0x0020);
speciesEng=speciesEng.trim();
other.speciesEng=other.speciesEng.trim();
Far from elegant but it works for the moment. I'll leave the question open for a couple of days in case anyone has more to add.
Thanks to all for the answers.
Why String.Equals is returning false?
CompareTo ignores certain characters:
static void Main(string[] args)
{
var a = "asdas"+(char)847;//add a hidden character
var b = "asdas";
Console.WriteLine(a.Equals(b)); //false
Console.WriteLine(a.CompareTo(b)); //0
Console.WriteLine(a.Length); //6
Console.WriteLine(b.Length); //5
//watch window shows both a and b as "asdas"
}
(Here, the character added to a
is U+034F
, Combining Grapheme Joiner.)
So CompareTo's result is not a good indicator of a bug in Equals. The most likely reason of your problem is hidden characters. You can check the lengths to be sure.
See this for more info.
EqualsIgnoreCase() not working as intended.
You are using/comparing the german ß sign, its uppercase produce SS... so you need to use the Locale.German
if (string1.toUpperCase(Locale.GERMAN).equals(string2.toUpperCase(Locale.GERMAN)))
that will return true....
Related Topics
Implementing the Producer/Consumer Pattern in C#
Patch Async Requests with Windows.Web.Http.Httpclient Class
Does Reactive Extensions Support Rolling Buffers
How to Have a Loop in a Windows Service Without Using the Timer
What's the Max Items in a List<T>
Get Window State of Another Process
Easiest Way to Parse "Querystring" Formatted Data
Copy Values from One Object to Another
How to Drag a Usercontrol Inside a Canvas
Setting Dropdownlist Selecteditem Programmatically
Asynchronous Iterator Task<Ienumerable<T>>
How to Drag and Drop a Button from One Panel to Another Panel