首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell -Unique转换为ae

Powershell -Unique转换为ae
EN

Stack Overflow用户
提问于 2022-06-15 05:48:02
回答 2查看 87关注 0票数 3

这是一个非常简单的例子

代码语言:javascript
运行
复制
$Test = @('ae','æ')
$Test | Select-Object -Unique

输出量

代码语言:javascript
运行
复制
ae

这里发生了什么,我该如何避免。显然,我不希望"ae“等于”ae“。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-15 10:01:17

正如注释中提到的,您当前的区域性设置将aeæ标识为相等,因此它只返回输入数组中的第一个。

如果您倒序,您将得到æ

代码语言:javascript
运行
复制
$Test = @('æ','ae')
$Test | Select-Object -Unique
# æ

您可以检查PowerShell与以下内容一起使用的区域性:

代码语言:javascript
运行
复制
PS> Get-Culture

LCID             Name             DisplayName
----             ----             -----------
2057             en-GB            English (United Kingdom)

尽管请注意,在@mclement0的评论中,PowerShell并没有一致地使用这种文化.

事实证明,当前的文化确实适用于Select(它目前也出人意料地(总是)区分大小写)。似乎PowerShell在文化不变性方面具有分裂的个性:字符串转换、字符串内插和与字符串相关的操作符(除了>)使用不变的区域性,而cmdlet使用当前的区域性。

无论如何,要了解更多的详细信息,请参阅序号串运算,而不是一种文化感知的比较,它听起来像是您所追求的是“序号”比较。

序数比较是指在没有语言解释的情况下对每个字符串的每个字节进行比较的字符串比较;例如,"Windows“与"windows”不匹配。

(扩展而言,ae并不等于æ)

在PowerShell中,我找不到一种惯用的方法来做到这一点(您可以用Set-Culture改变文化,但我尝试过的所有方法仍然将aeæ等同对待),但是如果您想要更多地控制如何比较值,您可以这样进入Linq:

代码语言:javascript
运行
复制
PS> $data = @( "ae", "æ" )
PS> [System.Linq.Enumerable]::Distinct([string[]]$data, [System.StringComparer]::Ordinal )
ae
æ

然后,您就有了大量不同的比较字符串的方法:

https://learn.microsoft.com/en-us/dotnet/api/system.stringcomparer?view=net-6.0#properties

  • CurrentCulture -获取一个StringComparer对象,该对象使用当前区域性的单词比较规则执行区分大小写的字符串比较。
  • CurrentCultureIgnoreCase -获取一个StringComparer对象,该对象使用当前区域性的单词比较规则执行不区分大小写的字符串比较。
  • InvariantCulture -获取一个StringComparer对象,该对象使用不变区域性的单词比较规则执行区分大小写的字符串比较。
  • InvariantCultureIgnoreCase -获取一个StringComparer对象,该对象使用不变区域性的单词比较规则执行不区分大小写的字符串比较。
  • ordinal -获取执行区分大小写序号字符串比较的StringComparer对象.
  • OrdinalIgnoreCase -获取执行不区分大小写序号字符串比较的StringComparer对象.

你甚至可以实现你自己的

代码语言:javascript
运行
复制
class FirstLetterComparer : System.Collections.Generic.IEqualityComparer[string] {
  [bool]Equals([string]$x, [string]$y) { return $x[0] -eq $y[0]; }
  [int]GetHashCode([string] $x) { return $x[0].GetHashCode(); }
}

# returns the first item in the list that starts with each distinct character.
# note that "abb" is omitted because it starts with the same first letter as "aaa"
# so it's not "first letter distinct".
$data = @( "aaa", "abb", "bbb" )
[System.Linq.Enumerable]::Distinct([string[]]$data, [FirstLetterComparer]::new() )
# aaa
# bbb
票数 3
EN

Stack Overflow用户

发布于 2022-06-16 00:11:02

若要添加到mclayton的好答案,请提供背景信息

  • 虽然对于cmdlet(如-eq PowerShell )确实使用当前区域性,但存在使用不变区域性的上下文,特别是 / PowerShellE 118操作符E 219--参见这个答案
  • PowerShell有两个不同的版本,它们在上的行为不同,这取决于它们构建在哪个版本的.NET上:
代码语言:javascript
运行
复制
- _Windows PowerShell_, the legacy, ships-with-Windows edition, whose latest and final version is 5.1, which builds on the legacy, Windows-only _.NET Framework_, which uses [**NSL (National Language Support)**](https://learn.microsoft.com/en-us/windows/win32/intl/national-language-support) for culture-specific information.
代码语言:javascript
运行
复制
- [_PowerShell (Core) 7+_](https://github.com/PowerShell/PowerShell/blob/master/README.md), which builds on the cross-platform _.NET 5+_ edition, which now uses the [**ICU (International Components for Unicode) library**](https://icu.unicode.org/) _by default_ - though [_on Windows_ you can opt-into still using NLS](https://learn.microsoft.com/en-us/dotnet/core/extensions/globalization-icu).

继续阅读,了解细节。

æ是由字母ae组成的结扎

  • Windows PowerShell / NLS
代码语言:javascript
运行
复制
- The **ligature** **`æ`** **is considered** _**equivalent**_ **to the sequence of its** _**constituent letters**_ **in** _**most**_ **cultures**, _**except**_ in those:
代码语言:javascript
运行
复制
    - where `æ` is in use as a character in its own right ...
    - _and_ is _not_ considered equivalent to the sequence of its constituent letters.
代码语言:javascript
运行
复制
- These exceptions are (only the so-called _neutral_ (non-nation-specific) cultures are listed, not also their national varieties):
代码语言:javascript
运行
复制
    - da (Danish)
    - is (Icelandic)
    - kl (Kalaallisut)
    - nb (Norwegian Bokmål)
    - nn (Norwegian Nynorsk)
    - no (Norwegian)
    - se (Northern Sami)
    - sma (Sami (Southern))
    - smj (Sami (Lule))
    - smn (Sami (Inari))
    - sms (Sami (Skolt))
代码语言:javascript
运行
复制
- **Other ligatures have multi-letter equivalents in** _**all**_ **cultures**, such as [`œ`](https://en.wikipedia.org/wiki/%C5%92) vs. `oe`; there are also ligatures whose multi-letter equivalent is _not_ the sequence of its constituent letters, but a modern equivalent, e.g., German [`ß`](https://en.wikipedia.org/wiki/%C3%9F) (which originated from `sz`) is considered equivalent to `ss`.
  • PowerShell (核心) 7+ / ICU
代码语言:javascript
运行
复制
- At least as of the ICU version that underlies PowerShell 7.2.4, **ligatures** _**in general**_ **are seemingly** _**never**_ **considered equivalent to their constituent letters** in string comparisons.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72626425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档