gpt4 book ai didi

swift - Swift 中的字符串比较是如何发生的

转载 作者:搜寻专家 更新时间:2023-11-01 05:33:58 25 4
gpt4 key购买 nike

在这个例子中:

var str1 = "hello"
var str2 = "Hello"

if str1 < str2 { print("hello is less than Hello")}
else {print("hello is more than Hello")}

根据什么发现str1大于str2?

最佳答案

Swift 字符串是根据 Unicode Collation Algorithm ,这意味着(有效地),

在您的示例中,"hello "和 "Hello" 具有 Unicode 值

hello: U+0068, U+0065, U+006C, U+006C, U+006F 
Hello: U+0048, U+0065, U+006C, U+006C, U+006F

因此 "Hello" < "hello" .

“规范化”或“分解”是相关的,例如对于字符带有变音符号。例如,

a = U+0061
ä = U+00E4
b = U+0062

分解后的形式

a: U+0061
ä: U+0061, U+0308 // LATIN SMALL LETTER A + COMBINING DIAERESIS
b: U+0062

因此 "a" < "ä" < "b" .

有关更多详细信息和示例,请参阅 What does it mean that string and character comparisons in Swift are not locale-sensitive?

关于swift - Swift 中的字符串比较是如何发生的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45990127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com