对不起,我是新来的斯威夫特。我想计算字符串中的目标字符。但是我不知道怎么做。有什么好的建议吗?谢谢。
let string = "hello\nNice to meet you.\nMy name is Leo.\n" //I want to get 3
最佳答案
如果您只需要计算换行字符数,则可以对字符串的字符使用筛选器:
let string = "hello\nNice to meet you.\nMy name is Leo.\n"
let count = string.characters.filter { $0 == "\n" }.count
print(count)
如预期,这项产出为3。
关于ios - 如何快速获取字符串中换行的频率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44171253/