C# 字符串比较返回false event,尽管它们相同
我有以下代码:C# 字符串比较返回false event,尽管它们相同,c#,sqlite,C#,Sqlite,我有以下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.W
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Threading;
using System.Windows.Threading;
using System.Data.SQLite;
using System.Text.RegularExpressions;
namespace SerialTrial
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public class ThreadExample
{
FlowDocument mcFlowDoc = new FlowDocument();
Paragraph para = new Paragraph();
public static void ThreadJob(MainWindow mainWindow)
{
string dBConnectionString = @"Data Source = C:\Users\Documents\Visual Studio 2012\Projects\SerialTrial\SerialTrial\bin\Debug\CompanyList.sqlite;";
SQLiteConnection sqliteCon = new SQLiteConnection(dBConnectionString);
//open connection to database
try
{
sqliteCon.Open();
SQLiteCommand createCommand = new SQLiteCommand("Select * from AllEmployee", sqliteCon);
SQLiteDataReader reader;
reader = createCommand.ExecuteReader();
int count = 0;
//richtextbox2.Document.Blocks.Clear();
List<string> mystring = new List<string>();
while (reader.Read())
{
count++;
string Text = (String.Format("{0}", Object.Equals(Variables.buffering, reader.GetString(0))));
Console.WriteLine(Text);
mystring.Add(Text);
if (Convert.ToBoolean(Text))
{
mystring.Add("1");
//string myText = new TextRange(mainWindow.richtextbox2.Document.ContentStart, mainWindow.richtextbox2.Document.ContentEnd).Text;
//replace two or more consecutive spaces with a single space, and
//replace two or more consecutive newlines with a single newline.
//var str = Regex.Replace(myText, @"( |\r?\n)\1+", "$1", RegexOptions.Multiline);
//Int64 id = reader.GetInt64(0);
string fname = reader.GetString(1);
string sname = reader.GetString(2);
DateTime saveNow = DateTime.Now;
mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new Run(saveNow.ToString())))));
mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(": ")));
mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(fname)));
mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(" ")));
mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(sname)));
mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(" granted access to gate 1")));
string text = "s";
mainWindow.WriteSerial(text);
//Console.WriteLine(Text);
//Console.WriteLine("ID = {0}, Name = {1}, Surname = {2}, Age = {3}", id, fname, sname, age);
//Console.WriteLine("{0}\t{1}\t{2}\t{3}", reader.GetInt64(0), reader.GetString(1), reader.GetString(2), reader.GetString(3));
}
}
sqliteCon.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public partial class MainWindow : Window
{
SerialPort serial = new SerialPort();
//string received_data;
//Thread readThread = new Thread(Read);
FlowDocument mcFlowDoc = new FlowDocument();
Paragraph para = new Paragraph();
public MainWindow()
{
InitializeComponent();
combobox1.Items.Insert(0, "Select Port");
combobox1.SelectedIndex = 0;
string[] ports = null;
ports = SerialPort.GetPortNames();
// Display each port name to the console.
int c = ports.Count();
for (int i = 1; i <= c; i++)
{
if (!combobox1.Items.Contains(ports[i - 1]))
{
combobox1.Items.Add(ports[i - 1]);
}
}
}
string dBConnectionString = @"Data Source = C:\Users\Documents\Visual Studio 2012\Projects\SerialTrial\SerialTrial\bin\Debug\CompanyList.sqlite;";
static int count = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
count++;
string[] ports = null;
ports = SerialPort.GetPortNames();
// Display each port name to the console.
int c = ports.Count();
for (int i = 1; i <= c; i++)
{
if (!combobox1.Items.Contains(ports[i - 1]))
{
combobox1.Items.Add(ports[i - 1]);
}
}
}
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int bytes = serial.BytesToRead;
Console.WriteLine(bytes);
if (bytes == 14)
{
byte[] buffer = new byte[bytes];
serial.Read(buffer, 0, bytes);
System.String ID = System.Text.Encoding.Default.GetString(buffer);
//System.String ID = new String(System.Text.Encoding.UTF8.GetChars(buffer));
Console.WriteLine(ID);
foreach (var item in ID)
{
Console.WriteLine(item.ToString());
}
/*
Console.WriteLine();
string s2 = BitConverter.ToString(buffer);
string tempAry = s2.Replace("-", "");
Console.WriteLine(tempAry);
Variables.buffering = BitConverter.ToInt64(buffer, 0);
*/
Variables.buffering = ID;
Console.WriteLine();
//Console.WriteLine(Variables.buffering);
Thread thread = new Thread(new ThreadStart(() => ThreadExample.ThreadJob(this)));
thread.Start();
thread.Join();
}
}
请帮忙。谢谢Object.Equals比较地址
Object.Equals(Variables.buffering, reader.GetString(0))
尝试使用字符串。等于比较字符串
编辑:正如下面的评论所指出的,这不仅不起作用,而且也是错误的,因此请忽略我的回答通常我们比较值类型变量或参考类型变量。如果您将int,string类型的变量与int,string进行比较,那么您应该选择值类型,如1=1或“a”=“b”,但当您与object进行比较时,您应该选择引用类型,如1.equal(1)或a.equal(b)您收到的数据是
0x02,0x37,0x30,0x30,0x32,0x42,0x34,0x30,0x38,0x45,0x39,0x35,0x03
。我猜0x02和0x03是一种开始和停止字节。它们之间是ASCII字符串。因此,您需要通过替换代码来忽略这些开始和停止字节:
System.String ID = System.Text.Encoding.Default.GetString(buffer);
与
错误发生在代码的何处?您已经提供了很多代码。。。你试过调试检查器告诉你的内容吗?要支持你相对简单的问题,代码太多了。您是否尝试过显示相同问题的非常小的控制台应用程序?在调用
对象.Equals
时,变量的具体内容是什么。缓冲和读取器.GetString(0)
?有哪些.ToCharArray()
输出?他们持有什么?另外:您应该在这里使用=
:变量。缓冲==reader.GetString(0)
您可以将开头的0x02和结尾的0x03转换为字符串。而如果转换为字符串,则希望跳过它们。尝试.GetString(buffer,1,12)
.Variables.buffering在类中使其全局化。没有错误,但输出不是我所期望的,因为我从串行接收的字符串在数据库中是相同的。我的代码如何返回false?这段代码是:stringtext=(string.Format({0}),Object.Equals(Variables.buffering,reader.GetString(0));我尝试更改为:string Text=(string.Format({0}),string.Equals(Variables.buffering,reader.GetString(0));--但如果使用Object.Equals with strings,它仍然返回False。它将调用string.Equals。那么我如何检查这两个字符串的相似性呢?一个来自串行端口,另一个来自我的sqlite数据库变量如何。buffering==reader.GetString(0),如上所述?object.Equals不比较地址。它执行被重写的等于
方法对提供的第一个实例执行的任何操作。引用类型的默认实现是比较引用,但只有在没有提供其他实现时才进行比较。字符串重写等于
,以提供值语义。
System.String ID = System.Text.Encoding.Default.GetString(buffer);
System.String ID = System.Text.Encoding.Default.GetString(buffer, 1, 12); // start reading from byte at 1st position and take 12 of them