Why is this string comparison not working? C
Besides the issues mentioned in the comments: you can't compare strings like that:
if(uAts == rline)
That wil compare the addresses of the char arrays and these will always be different. Use
if( strcmp( uAts, rline ) == 0 )
instead. strcmp()
will compare the contents of two NUL terminated char arrays (aka "strings" in C).
C# String comparison not working when compared with a value received over network
The problem was in the presence of an empty character in the end which failed the comparison.
When the Code.Length was checked it was 6 while the length of Admin was 5.
Thus the error was removed by taking a substring of the responseData without the last empty character.
public string ClientReceive()
{
try
{
stream = client.GetStream();
byte[] data;
data = new Byte[256];
String responseData = String.Empty;
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = Encoding.ASCII.GetString(data, 0, bytes);
responseData = responseData.Substring(0, responseData.Length-1); // This will remove the last character
return responseData;
}
catch (Exception ex)
{
DisplayAlert("Error", ex.Message, "OK");
return null;
}
}
Special Thanks to @bommelding and @Damien_The_Unbelievable for directing me to the correct path.
Java string comparison not working?
You have used the first comparison correctly, but later on, you got it the wrong way some how.
Always use equals()
method when comparing the content of strings, as you did in the first case.
Also, if its not working somehow, it may be because you have leading
or trailing
whitespace in your string. Try using it this way: -
if (grades[i].trim().equals("IG"))
Why comparing Strings in MySQL query is not working
First of all, I must acknowledge the points made by @Uueerdo were actually the the main cause of this issue. Even I was somewhat sure that there are some hidden characters in the string causing all the issue but I was not sure how to find and fix that offending character.
Also, the approach suggested by @Uueerdo to check and replace the offending character using the ASCII code seems quite legit but as he himself mentioned that this process will take lot's of time and one have to manually check every string for that one offending character and then replace it.
Luckily after spending couple of hours on it, I came up with a much faster approach to fix the issue. For that, first of all I would like to share my use case.
My first query was for selecting all the strings from a database and printing the result on page.
$result = mysqli_query($conn, "SELECT * from table_name");
while($row = mysqli_fetch_array($result)){
$string_var = $row["string_name"];
echo $string_var;
echo "<br>";
}
The above code was working as expected and printing all the string_name
from the table. Now, if I wanted to use the variable $string_var
for another SELECT query in the same table, it was giving me 0 results.
$result = mysqli_query($conn, "SELECT * FROM table_name");
while($row = mysqli_fetch_array($result)){
$string_var = $row["string_name"];
echo "String Name : ".$string_var."";
$sec_result = ($conn, "SELECT * FROM table_name WHERE string_var='$string_name'");
if(mysqli_num_rows($sec_result) > 0){
echo "Has Results";
} else {
echo "No Results";
}
}
In this snippet, my second query $sec_result
was always giving me No Results as output.
What I simply did to fix this issue.
$result = mysqli_query($conn, "SELECT * FROM table_name";
while ($row = mysqli_fetch_array($result)){
$string_var = $row["string_name"];
$row_id = $row["id"];
$update_row = mysqli_query($conn, "UPDATE table_name SET string_name='$string_var' WHERE id=$row_id");
}
This step updated all the strings from the table without any hidden/problem causing character.
I am not generalising this approach and I am not sure if this will work in every use case but it helped me fix my issue in less than a minute.
I request @Uueerdo and others with better understanding on this to post a more generic approach so that it can help others because I think many people who can't find a right approach in such conditions, end up using LIKE
in place of =
but that completely changes the core idea of the query.
Javascript String Compare == sometimes fails
Double equals is the appropriate way to compare strings in Javascript, it is returning false then there may be whitespace to the left and or right of one string.
Put a .trim()
on the end of the strings and my comparison should started working:
var panel = response.substr(0, response.indexOf("<")).trim();
if(panel == "combo"){
//do something
}
Why doesn't my string comparison work?
Your second comparison is wrong. You should also use equals
instead of ==
, like this:
if (action.trim().equals("something"))
The ==
operator compares references of (String) objects and under normal circumstances equal strings don't automatically have the same reference, i.e. they are different objects. (Unless both are internalized, but normally you shouldn't consider it)
Other than that your example works fine and the first comparison is valid. Try fixing the second comparison. If it works, you found your problem. If not, try using a debugger and double-check everything.
PS: When comparing literal strings with dynamic string objects, it's good practice to call the equals
method on the literal string:
"something".equals(action)
That way you can avoid NullPointerExceptions when the string object is null.
String comparison not working for text received from web scraping
Your html string has html entities
Dividends INR does not equal Dividends <span>INR
use https://pub.dartlang.org/packages/html_unescape to decode itemheader before you do comparison
python string comparison (==) not working
You seem to have a string processing error.PlayerId
seems to be a C-String being stored in a unicode String.
Background: C
uses a nullbyte (\x00
) to mark the end of a string. Since this nullbyte is in your string it ends up in the string representation of the object.
You can take a look here, for some reference. But without more code I am not sure about the cause/fix.
Have you tried type(playerId)
?
edit: I don't know what python implementation you are using, for cpython look here
Unfortunately I am not to firm in interfacing c and python, however you could try to use PyString_FromString
to convert it on the c side to a python string or use some handcrafted function (eg split with regex at the first unescaped 0).
Some interfacing libs are listed in this awnser
String comparison does not work in python
As it turns out, your string has new-lines (\n\n
) at the end.
You can use
text = text.strip()
to remove any surrounding whitespace from your string.
Related Topics
Difference Between Printf and Echo in Bash
Identifying Which Linux System Library Contains a Function
How to Set a Variable Used in a Perl Script as Environment Variable
Ffmpeg Fix Watermark Size or Percentage
How to Append Contents of Multiple Files into One File
How to Diff Directories Over Ssh
Limit the Memory and CPU Available for a User in Linux
How to Define a Bash Alias as a Sequence of Multiple Commands
Differencebetween .Got and .Got.Plt Section
How to Automate the Installation of Eclipse Plugins with Command Line
Command Not Found in Bash's If-Else Condition When Using [! -D "$Dir"]
Jboss as 7.1.1 Ejb 3:Ejb Pool Error
Shell Script for Multithreading a Process
Fast Linux File Count for a Large Number of Files
Does Os X Have an Equivalent to /Dev/Shm