繁体   English   中英

如何检测用户是否在 UITextField 中输入了省略号?

[英]How to detect if a user typed an ellipsis in a UITextField?

如何检查字符串在 swift 中是否有省略号? 在使用 swift 字符串函数时,我注意到一些奇怪的行为,并了解到省略号是罪魁祸首。 当用户在 UITextField 中输入...作为字符串的一部分时,然后我尝试定位一个字符串,之后字符数总是关闭 2。这是因为字符串函数在定位时将...视为 3 个字符我正在搜索的字符串的索引,但字符计数函数将其视为 1 个字符。 解决方案非常简单......当我在字符串中有一个椭圆时,然后将字符串的“找到”索引调整 2。问题是我不知道如何搜索“这个字符串是否有省略号”,因为这个没找到:

if heading.contains("...") {
            print ("found the ...!")
        }

我怀疑有一种特殊的方法可以搜索省略号,但无法找出它是什么。 这是我的“找到前 30 个字符后的最后一个空格” function 适用于没有省略号的字符串:

func breakOutHeadingFromString(fullString: String, charBreakPoint: Int) -> (heading: String, remainingText: String, breakChar: String) {
var heading = fullString
var remainingText = ""
var breakChar = ""

// If the Full string has less characters than break point then just return full blurb as heading and blank out 2
if fullString.count > charBreakPoint {
    // Get N characters out of total char count (hardcoded to 30 can this be dynamic?)
    var prefix = String(fullString.prefix(charBreakPoint))

    // Find the last space in heading so you can continue message there
    let lastSpace = prefix.lastIndex(of: " ") ?? prefix.endIndex
    var breakPoint = lastSpace
    breakChar = "|"

    // If there is a /n clip there
    if let newLine = prefix.firstIndex(of: "\n") {
        prefix = String(prefix[..<newLine])
        breakPoint = newLine
        breakChar = "\n"
    }
    // Use the Break Point to split the message in 2
    let breakPointInt: Int = fullString.distance(from: fullString.startIndex, to: breakPoint)
    // if the string has a eliptical ... then the break point is off by 2 because it 1 char in but 3 in
    heading = String(fullString.prefix(breakPointInt))
    remainingText = String(fullString.suffix(fullString.count - breakPointInt - 1))
}
return (heading,remainingText,breakChar)

}

省略号是 1 unicode 字符,而不是 3,所以它被算作 1 个字符,以下是我认为在您的情况下发生的情况。

这个没找到

if heading.contains("...") {
            print ("found the ...!")
        }

因为这些是 3 个句点(3 个字符)并且与省略号字符(1 个字符)不同

尝试用鼠标突出显示您正在比较的内容 (...) 与实际的省略号字符 (...)

在第一种情况下,您可以使用鼠标单独突出显示每个点,在第二种情况下,您将无法 select 每个单独的点。

这是一些测试:

var ellipsisString = "This is … a"
var threeDotString = "This is ... a"

print("Ellipsis character count: \(ellipsisString.count)")
print("Three dot character count: \(threeDotString.count)")

// The output:
Ellipsis character count: 11
Three dot character count: 13

如您所见,使用正确的省略号字符,它仅计为 1 个字符

现在使用您的contains function 和省略号字符串:

var ellipsisString = "This is … a"
print(ellipsisString.contains("…"))
print(ellipsisString.contains("..."))

// The output
true
false

您可以看到 contains("...") 使用真正的省略号字符成功,但 contains("...") 使用您使用的三个点失败。

最后,假设我想在省略号字符串中的省略号字符之后添加字符串nice This is … a - 如果使用了正确的省略号字符,您的策略将无法在索引中添加 2

这是我为实现这一目标所做的工作:

var ellipsisString = "This is … a"

// Find the index of the ellipsis character
if let ellipsisIndex = ellipsisString.firstIndex(of: "…")
{
// Convert the String index to a numeric value
let numericIndex: Int
= ellipsisString.distance(from: ellipsisString.startIndex,
                          to: ellipsisIndex)

// Ellipsis located at the 8th index which is right
print(numericIndex)

// Set the index where we want to add the new text
// The index after the ellipsis character so offset by 1
let insertIndex = ellipsisString.index(ellipsisIndex,
                           offsetBy: 1)

// Insert the text " nice" into the string after the
// ellipsis character
ellipsisString.insert(contentsOf: " nice",
          at: insertIndex)
}

// Print the output after the addition
print(ellipsisString)

// Output
This is … nice a

这给出了您希望的所需 output,即准确地找到省略号字符的 position,然后使用该 position 来执行您想要的操作,在我的情况下,添加文本

希望这可以为您解决一些问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM