Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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# 比较以null结尾的字符串与以非null结尾的字符串_C#_String - Fatal编程技术网

C# 比较以null结尾的字符串与以非null结尾的字符串

C# 比较以null结尾的字符串与以非null结尾的字符串,c#,string,C#,String,我使用System.Drawing.Imaging从图像目录中提取图像标记。我可以很好地提取标记,但问题是PropertyItem中的字符串以null结尾,而我比较它的字符串不是。我的问题是,如何比较C#中以null结尾的字符串和以非null结尾的字符串 这是我正在运行的代码 byte[] tagBytes = new byte[tag.Length * sizeof(char)]; System.Buffer.BlockCopy(tag.ToCharArray(), 0, tagBytes,

我使用System.Drawing.Imaging从图像目录中提取图像标记。我可以很好地提取标记,但问题是PropertyItem中的字符串以null结尾,而我比较它的字符串不是。我的问题是,如何比较C#中以null结尾的字符串和以非null结尾的字符串

这是我正在运行的代码

byte[] tagBytes = new byte[tag.Length * sizeof(char)];
System.Buffer.BlockCopy(tag.ToCharArray(), 0, tagBytes, 0, tagBytes.Length);

pnl_slider.Visible = false;
string relativePath = ConfigurationManager.AppSettings["PhotoPathRelative"];
DirectoryInfo info = new DirectoryInfo(ConfigurationManager.AppSettings["PhotoPathPhysical"]);
FileInfo[] Files = info.GetFiles("*.jpg").OrderBy(p => p.CreationTime).ToArray();
foreach (FileInfo file in Files)
{
    System.Drawing.Image img = new Bitmap(file.FullName);
    foreach (PropertyItem item in img.PropertyItems)
    {
        if (item.Id == 40094)
        {
            str += "'" + Encoding.Unicode.GetString(item.Value).Trim() + "' " + BitConverter.ToString(item.Value) + " == " + BitConverter.ToString(tagBytes) + "<br/>";
        }
    }
    img.Dispose();
}

手动修剪空值实现了如下回答所示的技巧:


只需调用
Encoding.Unicode.GetString(item.Value).Trim()
在字节数组上,输出字符串应该是相同的,您可以在输出中看到最后一个
00-00
已被解码器剥离。@kennyzx我尝试了一下,没有骰子,同样的问题
if(Encoding.Unicode.GetString(item.Value).Trim()==Encoding.Unicode.GetString(tagBytes.Trim())
看起来您必须修剪终止符,我以为这是由解码器完成的,但我错了。这就成功了。谢谢你的帮助。
'wildlife' 77-00-69-00-6C-00-64-00-6C-00-69-00-66-00-65-00-00-00 == 77-00-69-00-6C-00-64-00-6C-00-69-00-66-00-65-00
'wildlife' 77-00-69-00-6C-00-64-00-6C-00-69-00-66-00-65-00-00-00 == 77-00-69-00-6C-00-64-00-6C-00-69-00-66-00-65-00
'wildlife' 77-00-69-00-6C-00-64-00-6C-00-69-00-66-00-65-00-00-00 == 77-00-69-00-6C-00-64-00-6C-00-69-00-66-00-65-00
'macro' 6D-00-61-00-63-00-72-00-6F-00-00-00 == 77-00-69-00-6C-00-64-00-6C-00-69-00-66-00-65-00