.net - 无法在 PowerShell 中将字符串转换为 int

标签 .net powershell type-conversion

我正在尝试使用 PowerShell 将字符串转换为整数。但是,它一直告诉我我没有有效号码,即使我确定我有。
首先,这里是我如何获取我的变量,以及类型的打印输出等,只是为了确保有效性:

$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 ‏‎8:09 AM

5/26/2016

5

System.String


这是我尝试转换的第一种方法:
[int]$converted = 0
[int]::TryParse($intVar, [ref]$converted)
Write-Host $converted
和输出:

False

0


下一个方法:
$converted = [convert]::ToInt32($intVar, 10)
结果:

Exception calling "ToInt32" with "2" argument(s): "Could not find any recognizable digits."


我试过的第三种方法:
$converted = $intVar / 1
结果:

Cannot convert value "‎5" to type "System.Int32". Error: "Input string was not in a correct format."


如果我手动分配 $intVar值“5”( $intVar = "5" )一切正常,所以我认为我如何获得值(value)一定存在问题。但我不知道我做错了什么,因为 GetType()说它确实是一个字符串。
编辑:根据 TobyU 的回答,我也试过 $intVar = [int]$intVar ,同样的结果

Cannot convert value "‎5" to type "System.Int32". Error: "Input string was not in a correct format."


编辑:还有一种方法:$intVar = [int]::Parse($intVar)这使:

Exception calling "Parse" with "1" argument(s): "Input string was not in a correct format."


编辑 3:显然,正如一些评论者所提到的,存在无效字符。这是 Format-Hex 的输出:
           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   3F 32 36                                         ?26             

最佳答案

检查问题源文本中的错误消息显示 您的字符串包含不可见的 LEFT-TO-RIGHT-MARK Unicode character ( U+200E ) ,这就是转换失败的原因。

删除该字符将使转换成功,在最简单的情况下,只需删除所有非数字字符即可实现。从字符串:

# 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 ,但格式在视觉上不容易解析:
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            
-Encoding BigEndianUnicode使用 (UTF16-BE) - 即使 .NET 字符串使用 Unicode (UTF16-LE) - 这样总是以字节为导向的显示首先显示 16 位代码单元的高字节,这样读起来更自然。

字节对 20 0E是第一个代码单元,U+200E (从左到右的标记)和 00 35第二个,U+0035 (数字 5 )。

右侧打印的字符的用处有限,因为它们是输入字节的逐字节解释,仅按预期呈现 8 位范围内的字符(代码点 <= U+00FF ); 0x0字节表示为 .

关于.net - 无法在 PowerShell 中将字符串转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55006049/

相关文章:

c# - 如何在asp.net中使用Page_Init

windows - 如何从 Get-ChildItem 获取所有可执行文件的路径

c# - WPF TextBox 事件 TextChanged PreviewTextInput 不触发

.net - 默认 .NET 运行时版本

Powershell:所有新对象都不是平等的吗?

c++ - 在 C++ 中将 N 位 int 表示为十六进制

r - 将一年中的几周转换为日期

Java - 从字符串类型转换为泛型类型

c# - Linq 结果 Int 错误 : Specified cast is not valid

arrays - PowerShell 阵列未移位