问题描述
我正在尝试使用PowerShell将字符串转换为整数.但是,即使我确定知道,我仍然会告诉我我没有有效的电话号码.
I'm attempting to convert a string to an integer using PowerShell. However, it keeps on telling me that I don't have a valid number, even though I'm sure I do.
首先,这是我获取变量的方式以及类型的打印输出等,只是为了确保有效性:
First of all, here's how I'm getting my variable, and a printout of the type, etc. just to ensure validity:
$obj = (New-Object -TypeName PSCustomObject -Property @{
LastSaved = $com.GetDetailsOf($_, 155).toString().trim()
})
Write-Host $obj.LastSaved
$datePart,$b,$c = $obj.LastSaved.Split(" ")
Write-Host $datePart
$intVar,$b,$c = $datePart.Split("/")
$intVar = $intVar.Trim()
$intVar -replace '\W', ''
Write-Host $intVar
Write-Host $intVar.GetType()
输出:
5/26/2016
5/26/2016
5
System.String
System.String
这是我尝试进行转换的第一个方法:
Here's the first method I've tried for conversion:
[int]$converted = 0
[int]::TryParse($intVar, [ref]$converted)
Write-Host $converted
输出:
0
下一个方法:
$converted = [convert]::ToInt32($intVar, 10)
结果:
还有我尝试过的第三种方法:
And the third method I've tried:
$converted = $intVar / 1
结果:
如果我手动分配 $ intVar
值为"5",( $ intVar ="5"
)一切都很好,所以我认为如何获取价值肯定存在问题.但是我不知道该怎么做,因为 GetType()
说它确实是一个字符串.
If I manually assign $intVar
a value of "5" ($intVar = "5"
) everything works just fine, so I think there must be an issue with how I'm getting the value. But I have no idea what I could be doing wrong, as the GetType()
says it is indeed a string.
编辑:根据TobyU的回答,我还尝试过 $ intVar = [int] $ intVar
,结果相同
Per TobyU's answer, I've also tried $intVar = [int]$intVar
, with the same result of
还有另一种方法:
$ intVar = [int] :: Parse($ intVar)
哪个给:
显然,正如一些评论者所述,其中包含无效字符.这是 Format-Hex
的输出:
EDIT 3: So apparently, as some commenters mentioned, there are invalid characters. Here is the output of a Format-Hex
:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 3F 32 36 ?26
推荐答案
检查问题源文本中的错误消息会发现您的字符串包含不可见的从左到右标记Unicode字符( U + 200E
) ,这就是转换失败的原因.
Examining the error messages in your question's source text reveals that your string contains the invisible LEFT-TO-RIGHT-MARK Unicode character (U+200E
), which is why the conversion fails.
删除该字符将使转换成功,在最简单的情况下,这是通过简单地消除所有非数字字符来实现的.从字符串中:
Removing that character will make the conversion succeed, which in the simplest case is achieved by simply eliminating all non-digit chars. from the string:
# Simulate the input string with the invisible control char.
$intStr = [char] 0x200e + '5'
# FAILS, due to the invisible Unicode char.
[int] $intStr # -> ... "Input string was not in a correct format."
# OK - eliminate non-digits first.
# Note the required (...) for proper precedence.
[int] ($intStr -replace '\D') # -> 5
可选阅读:检查字符串的字符:
# Print the code points of the string's characters:
PS> [int[]] [char[]] $intStr
8206 # decimal equivalent of 0x200e, the LEFT-TO-RIGHT-MARK
53 # decimal equivalent of 0x54, the DIGIT FIVE
# Show the code points in hex. format and print the char.
PS> [char[]] $intStr |
Select-Object @{ n='CodePoint'; e={ 'U+{0}' -f ([int] $_).ToString('X4') } },
@{ n='Char'; e={ $_ } }
CodePoint Char
--------- ----
U+200E
U+0035 5
您也可以使用 Format-Hex
,但是格式不易以视觉方式解析:
You can also use Format-Hex
, but the format isn't easy to parse visually:
PS> $intStr | Format-Hex -Encoding BigEndianUnicode
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000000000000000 20 0E 00 35 ..5
使用
-编码BigEndianUnicode
(UTF16-BE)-即使.NET字符串使用 Unicode
(UTF16-LE)-因此, byte 的显示屏首先显示16位代码单元的高字节,这样读取起来更自然.
-Encoding BigEndianUnicode
(UTF16-BE) is used - even though .NET string use Unicode
(UTF16-LE) - so that the invariably byte-oriented display shows the high byte of the 16-bit code units first, which reads more naturally.
字节对 20 0E
是第一个代码单元, U + 200E
(从左到右的标记)和 00 35
第二个是 U + 0035
(数字 5
).
Byte pair 20 0E
is the first code unit, U+200E
(the left-to-right mark), and 00 35
the second one, U+0035
(the digit 5
).
右侧打印的字符用途有限,因为它们是输入字节的 byte 单个解释,只能解释预期的8位范围内的字符(代码点<; = U + 00FF
); 0x0
字节表示为.
The printed characters to the right are of limited usefulness, because they are the byte-individual interpretation of the input bytes, which only renders characters in the 8-bit range as expected (code points <= U+00FF
); a 0x0
byte is represented as a .
这篇关于无法在PowerShell中将字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!