PowerShell 中存储在变量中的字符串的意外行为 [英] Unexpected behavior with a string stored in a variable in PowerShell

查看:33
本文介绍了PowerShell 中存储在变量中的字符串的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Excel 的 Cells.Find() 方法中得到了一些奇怪的行为:

I'm getting some odd behavior from Excel's Cells.Find() method:

我正在搜索的变量:

PS > $volumename
vol_01       

PS > $volumename.GetType()

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

不产生任何结果:

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 

获取我期望的值:

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.

推荐答案

以下代码段可用于检查字符串中的隐藏控制字符:

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 [
]

第一列是每个字符的 Unicode 代码点(ASCII 码"),第二列是字符本身,用 [...]

The first column is each character's Unicode code point ("ASCII code"), and the second column the character itself, enclosed in [...]

请注意,我在字符串末尾添加了 "`n" - a 换行符 (U+000A) - 其代码点表示为十六进制.数字是 0xa.

Note that I've added "`n" at the end of the string - a newline character (U+000A) - whose code point expressed as a hex. number is 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

在您的情况下,尾随空格是 0xa0NO-BREAK SPACE (U+00A0).TrimEnd() 也将其删除,如 Tom Blodget 指出.

In your case, the trailing whitespace is 0xa0, the NO-BREAK SPACE (U+00A0), which .TrimEnd() also removes, as Tom Blodget points out.

简单的函数包装器基于上述内容,用于管道输入:

Simple function wrapper based on the above, for use with pipeline input:

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

示例使用:

"vol_01`n" | debug-Chars

这篇关于PowerShell 中存储在变量中的字符串的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆