简体   繁体   English

存储在PowerShell中的变量中的字符串出现意外行为

[英]Unexpected behavior with a string stored in a variable in PowerShell

I'm getting some odd behavior from Excel's Cells.Find() method: 我从Excel的Cells.Find()方法中得到了一些奇怪的行为:

Variable I'm searching on: 我正在搜索的变量:

PS > $volumename
vol_01       

PS > $volumename.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                            
-------- -------- ----                                     --------                                                                                                                                            
True     True     String                                   System.Object 

produces no results: 没有结果:

PS > $sheet.Cells.Find($volumename).Row

but if I manually copy and paste the value of that variable: 但是如果我手动复制并粘贴该变量的值:

PS > $volumename = "vol_01"
PS > $volumename.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                            
-------- -------- ----                                     --------                                                                                                                                            
True     True     String                                   System.Object 

Gets the value I am expecting: 获得我期望的值:

PS > $sheet.Cells.Find($volumename).Row
198

They appear to be exactly the same type in every way to me. 在我看来,它们似乎是完全相同的类型。 This doesn't happen for every case. 并非在所有情况下都如此。 Some volume names passthrough fine while others do not. 一些卷名可以很好地通过,而另一些则不能。 I did scrub the volume name for this post as it has a customers naming convention. 我确实清除了该帖子的卷名,因为它具有客户命名约定。 It is the same format as above and the same format as the volume names that work. 它与上述格式相同,并且与有效的卷名相同。

The following snippet can be used to inspect a string for hidden control characters : 以下代码段可用于检查字符串中是否包含隐藏的控制字符

PS> & { [int[]] [char[]] $Args[0] | % { '0x{0:x} [{1}]' -f $_, [char] $_ } } "vol_01`n"
0x76 [v]
0x6f [o]
0x6c [l]
0x5f [_]
0x30 [0]
0x31 [1]
0xa [
]

The first column is each character's Unicode code point ("ASCII code"), and the second column the character itself, enclosed in [...] 第一列是每个字符的Unicode代码点(“ ASCII代码”),第二列是字符本身,用[...]括起来

Note that I've added "`n" at the end of the string - a newline character ( U+000A ) - whose code point expressed as a hex. 请注意,我在字符串的末尾添加了"`n" -一个换行符( U+000A -其代码点表示为十六进制。 number is 0xa . 数字是0xa

If, as in your case, the only unwanted part of the string is trailing whitespace , you can remove them as follows: 如果像您这样,字符串中唯一不需要的部分是尾随空格 ,则可以按以下方式删除它们:

$volumename.TrimEnd() # trim trailing whitespace

In your case, the trailing whitespace is 0xa0 , the NO-BREAK SPACE ( U+00A0 ) , which .TrimEnd() also removes, as Tom Blodget points out. 在您的情况下,尾随空格为0xa0 ,即NO-BREAK SPACE( U+00A0 ,正如Tom Blodget指出的那样, .TrimEnd()也将其删除。


Simple function wrapper based on the above, for use with pipeline input: 基于上述的简单函数包装器 ,用于管道输入:

filter debug-Chars { [int[]] [char[]] $_ | % { '0x{0:x} [{1}]' -f $_, [char] $_ } }

Sample use: 样品使用:

"vol_01`n" | debug-Chars

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM