【问题标题】:Equivalent of Javascripts encodeURI in Powershell?等效于 Powershell 中的 Javascript encodeURI?
【发布时间】:2019-11-29 18:50:50
【问题描述】:

在 Powershell 中 Javascripts encodeURI() / encodURIComponent() 的等价物是什么?

我正在编码一个 URL(其中需要一些 %20s),我讨厌手动进行。

【问题讨论】:

  • 您可以使用 URI 类中的方法。 [uri]::EscapeUriString('https://exmaple.com/string with spaces')

标签: javascript powershell encodeuricomponent


【解决方案1】:

对于这些情况,您可以使用 System.Uri 类。

encodeURI() 等效项是使用类中的 EscapeUriString 静态方法或将 URI 字符串转换为 System.URI 类型并访问 AbsoluteUri 属性。

$uri = 'https://example.com/string with spaces'
# Method 1
[uri]::EscapeUriString($uri)
# Method 2
([uri]$uri).AbsoluteUri

# Output
https://example.com/string%20with%20spaces

encodeURIComponent() 等效项可以使用类的 EscapeDataString 方法完成。

$uri = 'https://example.com/string with space&OtherThings=?'
[uri]::EscapeDataString($uri)

#Output

https%3A%2F%2Fexample.com%2Fstring%20with%20space%26OtherThings%3D%3F

注意:您不必定义变量(在本例中为$uri)。您可以将其替换为带引号的字符串。我仅出于可读性目的使用该变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多