Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么string.contains()返回false?_C#_.net_String - Fatal编程技术网

C# 为什么string.contains()返回false?

C# 为什么string.contains()返回false?,c#,.net,string,C#,.net,String,我有以下字符串:\\\?\hid#vid#u 04d8pid#u 003f#62edf110800000{4d1e55b2-f16f-11cf-88cb-001111000030}存储在名为devPathName 定义如下:const string myDevice=@“vid_04d8pid_003f” 但以下代码的计算结果始终为false: Boolean test = true; test = devPathName.Contains(myDevice); statusLabel.Te

我有以下字符串:
\\\?\hid#vid#u 04d8pid#u 003f#62edf110800000{4d1e55b2-f16f-11cf-88cb-001111000030}
存储在名为
devPathName

定义如下:
const string myDevice=@“vid_04d8pid_003f”

但以下代码的计算结果始终为false:

Boolean test = true;

test = devPathName.Contains(myDevice);

statusLabel.Text += "\n\tThe value of test: " + test.ToString();

当我将代码插入C#时,编译器不喜欢长字符串中的“\h”部分。上面写着“无法识别的逃逸序列”。这可能是你的问题吗

哦,如果我在长字符串前面加上“@”,contains()方法将返回true

嗯,


-Dan

当我将代码插入C#时,编译器不喜欢长字符串中的“\h”部分。上面写着“无法识别的逃逸序列”。这可能是你的问题吗

哦,如果我在长字符串前面加上“@”,contains()方法将返回true

嗯,


-Dan

假设您已经检查了字母“O”与数字“0”以及类似的内容,我建议您在字符串变量
devPathName
中看到的字符串正在被编码以显示,与您想象的不完全一样

例如,如果字符串包含字符
\x000d
Control-M
),则在检查字符串值时,Visual Studio调试器会将其显示为
\r

或者,再举一个例子,如果字符串包含序列3504(三五零四),但您搜索的是
35O4
(三五零四),那么您将找不到匹配项


(根据您的字体,您可能看不到某些字符之间的差异。比较不同字体中的“0O1lB8S5”与“
0O1lB8S5
”以了解我的意思。)

假设您已经检查了字母“O”与数字“0”以及类似的内容,我建议您在字符串变量
devPathName
中看到的字符串正在被编码以便显示,它与您想象的不完全一样

例如,如果字符串包含字符
\x000d
Control-M
),则在检查字符串值时,Visual Studio调试器会将其显示为
\r

或者,再举一个例子,如果字符串包含序列3504(三五零四),但您搜索的是
35O4
(三五零四),那么您将找不到匹配项


(根据您的字体,您可能看不到某些字符之间的差异。请将不同字体中的“0O1lB8S5”与“
0O1lB8S5
”进行比较,以了解我的意思。)

这对我来说也是正确的


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            const string myDevice = @"vid_04d8pid_003f";
            string devPathName = @"\\\?\hid#vid_04d8pid_003f#62edf110800000#{4d1e55b2-f16f-11cf-88cb-001111000030}";
            Boolean test =devPathName.Contains(myDevice);
            MessageBox.Show("\n\tThe value of test: " + test.ToString());
        }
    }
}

我想我应该设置一个断点,确保devPathName没有在您身上更改,并且该值是您期望的值。

这对我来说也是真的


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            const string myDevice = @"vid_04d8pid_003f";
            string devPathName = @"\\\?\hid#vid_04d8pid_003f#62edf110800000#{4d1e55b2-f16f-11cf-88cb-001111000030}";
            Boolean test =devPathName.Contains(myDevice);
            MessageBox.Show("\n\tThe value of test: " + test.ToString());
        }
    }
}

我想我应该在中设置一个断点,并确保devPathName没有在您身上更改,并且该值是您期望的值。

什么计算为false,您的测试变量或statusLabel.text中显示的一部分?您确定显示器不是这样的吗:

The value of test: false
The value of test: false
The value of test: false
The value of test: false
The value of test: true
The value of test: false

您可能只看到该字符串的第一行?

什么值为false,您的测试变量或statusLabel.text中的部分显示?您确定显示器不是这样的吗:

The value of test: false
The value of test: false
The value of test: false
The value of test: false
The value of test: true
The value of test: false

也许您只看到了该字符串的第一行?

您只需要在通过.Contains()解析长字符串之前对其进行转义

如前面的几个答案所示,在字符串的开头放一个at符号(@),它的计算结果应该是完美的

当进入.Contains时,初始字符串(在您的情况下)

\\\?\hid#vid_04d8pid_003f#62edf110800000#{4d1e55b2-f16f-11cf-88cb-001111000030}"

包含非转义字符和原因。contains在完成比较之前终止。

在通过.contains()解析长字符串之前,只需转义它

如前面的几个答案所示,在字符串的开头放一个at符号(@),它的计算结果应该是完美的

当进入.Contains时,初始字符串(在您的情况下)

\\\?\hid#vid_04d8pid_003f#62edf110800000#{4d1e55b2-f16f-11cf-88cb-001111000030}"

包含非转义字符和原因。包含在完成比较之前终止。

Hhmmm。。。。你是说我需要用Unicode编码转换它吗?我实际上从一个IntPtr(
devPathName=Marshal.PtrToStringAuto(pdevPathName);
)中检索到了这个字符串,但是您能解释一下您的意思吗?Thanks@dark-star1-更新了我的答案,希望这更清楚。你和Pat似乎一针见血。正在尝试使用路径。*引发异常,显示其中某处有“\r”和“\n”。我猜这是在返回字符串的开头。有没有办法绕过这一步,或者我必须复制字符串,修改它,然后为我的子字符串测试它?一个很好的经验法则是始终检查来自应用程序外部的输入,无论是否由用户直接输入。作为第一步,您可以使用Trim()删除前导空格和训练空格,但如果这是我的代码,我会更严格一些。您必须原谅我的草率。我正在同时学习C#和VisualStudio2008Express环境。我总是避免在windows上编码,这是有充分理由的。无论如何,我发现字符串是这样构造的:一个“&”用于聚合字符串中的每一条信息。尽管此字符不显示。是这个小东西把我甩了。。。。你是说我需要用Unicode编码转换它吗?我实际上从一个IntPtr(
devPathName=Marshal.PtrToStringAuto(pdevPathName);
)中检索到了这个字符串,但是您能解释一下您的意思吗?Thanks@dark-star1-更新了我的答案,希望这更清楚。你和Pat似乎一针见血。正在尝试使用路径。*引发异常,显示其中某处有“\r”和“\n”。我是