首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较here-string和get-content,compare-object表示相同的字符串是不同的

比较here-string和get-content,compare-object表示相同的字符串是不同的
EN

Stack Overflow用户
提问于 2021-05-22 18:36:37
回答 1查看 76关注 0票数 0

我使用compare-object通过"get-content“(difference对象)将here-string (引用对象)与文件内容进行比较。辅助指示器显示每个源中的第一行是不同的,尽管它们看起来是相同的,并且我手动删除了两个源中的所有空格。文件内容是用here-string本身创建的,所以我不明白为什么它认为第一行不同:

代码语言:javascript
复制
$GitIgnorePath = 'file'
$NewGitIgnore = @"
#  Local .terraform directories
**/.terraform/*
123
"@

Set-Content -path $GitIgnorePath -value $NewGitIgnore
$CurrentGitIgnore = Get-Content -Path $GitIgnorePath -Raw
Compare-Object $NewGitIgnore $CurrentGitIgnore


InputObject                                            SideIndicator
-----------       
#  Local .terraform directories...                          =>
#  Local .terraform directories...                          <=

如果我将$NewGitIgnore中的'123‘更改为"1234“,并再次运行compare-object,我会得到相同的结果。

EN

回答 1

Stack Overflow用户

发布于 2021-05-22 22:56:37

文件中的$currentgitIgnore字符串末尾有一个额外的/r/n (0D 0A)。Set-content -nonewline将摆脱这一点。$NewGitIgnore没有在末尾结束的行。它们每个都是一个字符串,这是不寻常的,不是字符串数组。该文件的格式也不正确,除最后一行外,所有行都以unix行结尾(仅\n)。

代码语言:javascript
复制
$NewGitIgnore | format-hex

   Label: String (System.String) <3CF480C1>

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 23 20 20 4C 6F 63 61 6C 20 2E 74 65 72 72 61 66 #  Local .terraf
0000000000000010 6F 72 6D 20 64 69 72 65 63 74 6F 72 69 65 73 0A orm directories�
0000000000000020 2A 2A 2F 2E 74 65 72 72 61 66 6F 72 6D 2F 2A 0A **/.terraform/*�
0000000000000030 31 32 33                                        123


$currentgitIgnore | format-hex

   Label: String (System.String) <5B75072F>

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 23 20 20 4C 6F 63 61 6C 20 2E 74 65 72 72 61 66 #  Local .terraf
0000000000000010 6F 72 6D 20 64 69 72 65 63 74 6F 72 69 65 73 0A orm directories�
0000000000000020 2A 2A 2F 2E 74 65 72 72 61 66 6F 72 6D 2F 2A 0A **/.terraform/*�
0000000000000030 31 32 33 0D 0A                                  123��

更典型的字符串数组比较如下所示,字符串数组相等。该文件将以windows文本形式结束(/r/n在每行的末尾)。请注意,使用compare-object的默认无限同步窗口,行的顺序并不重要。

代码语言:javascript
复制
$GitIgnorePath = 'file'
$NewGitIgnore = '#  Local .terraform directories',
'**/.terraform/*',
'123'

Set-Content -path $GitIgnorePath -value $NewGitIgnore
$CurrentGitIgnore = Get-Content -Path $GitIgnorePath

Compare-Object $NewGitIgnore $CurrentGitIgnore -IncludeEqual

InputObject                     SideIndicator
-----------                     -------------
#  Local .terraform directories ==
**/.terraform/*                 ==
123                             ==
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67648521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档