比较两个字符串的方法永远不会返回正确的结果

发布于 2024-10-17 08:14:40 字数 1591 浏览 3 评论 0原文

这是一个独立的问题,与我在此提出的上一个问题有关。

我已在 gist.github 发布了完整源代码的副本,但我只有一个挥之不去的问题我无法解决。

FindLine() 始终返回 -1。我已经确认这两个变量正在传递良好的数据,所以我无法弄清楚为什么比较永远不会返回 -1 以外的任何内容。

如果您不想查看完整的源代码,这里是相关代码的片段:

DataTable resultTable = new DataTable();
string ImportPath = @"***PATH***\***INFILE***.csv";
string QueryString = "SELECT DISTINCT UPPER(MP.Symbol) AS Symbol, LOWER(MP.SecType) AS SecType, MBI.Status FROM MoxySecMaster AS MP LEFT JOIN MoxyBondInfo AS MBI ON MP.Symbol = MBI.Symbol AND MP.SecType = MBI.SecType WHERE MP.SecType <> 'caus' AND MP.SecType IS NOT NULL AND MP.Symbol IS NOT NULL ORDER BY Symbol ASC;";
string symb = "";
StringBuilder OrigText = new StringBuilder();
SqlConnection MoxyConn = new SqlConnection("server=***;database=***;user id=***;password=***");
SqlDataAdapter adapter = new SqlDataAdapter(QueryString, MoxyConn);

MoxyConn.Open();
resultTable = new DataTable();
adapter.Fill(resultTable);
MoxyConn.Close();
OrigText.Append(File.ReadAllText(ImportPath));

char[] tempSymb = new char[10];
OrigText.CopyTo(0, tempSymb, 0, OrigText.ToString().IndexOf(",", 0));
symb = new string(tempSymb);
int foundSpot = FindLine(symb, resultTable);

...和...

static int FindLine(string symbol, DataTable symbolList)
{
    for (int vcl = 0; vcl < symbolList.Rows.Count; vcl++)
    {
        if (symbolList.Rows[vcl][0].ToString() == symbol.ToUpper())
            return vcl;
    }
    return -1;
}

This is a separate problem lingering from a previous question I asked here on SO.

I've posted a copy of my full source at gist.github and I only have one lingering problem that I can't resolve.

FindLine() always returns -1. I have confirmed that the two variables are getting passed good data, so I can't figure out why the comparisons never return anything other than -1.

Here's a snippet of the relevant code if you don't want to check out the full source:

DataTable resultTable = new DataTable();
string ImportPath = @"***PATH***\***INFILE***.csv";
string QueryString = "SELECT DISTINCT UPPER(MP.Symbol) AS Symbol, LOWER(MP.SecType) AS SecType, MBI.Status FROM MoxySecMaster AS MP LEFT JOIN MoxyBondInfo AS MBI ON MP.Symbol = MBI.Symbol AND MP.SecType = MBI.SecType WHERE MP.SecType <> 'caus' AND MP.SecType IS NOT NULL AND MP.Symbol IS NOT NULL ORDER BY Symbol ASC;";
string symb = "";
StringBuilder OrigText = new StringBuilder();
SqlConnection MoxyConn = new SqlConnection("server=***;database=***;user id=***;password=***");
SqlDataAdapter adapter = new SqlDataAdapter(QueryString, MoxyConn);

MoxyConn.Open();
resultTable = new DataTable();
adapter.Fill(resultTable);
MoxyConn.Close();
OrigText.Append(File.ReadAllText(ImportPath));

char[] tempSymb = new char[10];
OrigText.CopyTo(0, tempSymb, 0, OrigText.ToString().IndexOf(",", 0));
symb = new string(tempSymb);
int foundSpot = FindLine(symb, resultTable);

... and ...

static int FindLine(string symbol, DataTable symbolList)
{
    for (int vcl = 0; vcl < symbolList.Rows.Count; vcl++)
    {
        if (symbolList.Rows[vcl][0].ToString() == symbol.ToUpper())
            return vcl;
    }
    return -1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那小子欠揍 2024-10-24 08:14:40

尝试使用这个:

if (String.Equals(symbolList.Rows[vcl][0].ToString().Trim(),
   symbol, StringComparison.InvariantCultureIgnoreCase))

try using this:

if (String.Equals(symbolList.Rows[vcl][0].ToString().Trim(),
   symbol, StringComparison.InvariantCultureIgnoreCase))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文