用Kotlin枚举String中的单词

时间:2019-07-01 17:05:51

标签: android kotlin

iOS SDK提供了String API

func enumerateSubstrings<R>(in range: R, options opts: String.EnumerationOptions = [], _ body: @escaping (String?, Range<String.Index>, Range<String.Index>, inout Bool) -> Void) where R : RangeExpression, R.Bound == String.Index

其中一个选项是.byWords,它允许您枚举String中的单词。您还可以使用.reverse来向后枚举。

我也需要在Android中执行此操作。用例是提取较大字符串中的子字符串,并在其前面添加x个单词,并在其后面添加y个单词。例如,给定Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.和短语do eiusmod tempor incididunt,其中x = 1y = 2,创建结果字符串sed do eiusmod tempor incididunt ut labore

到目前为止,我正在提取匹配范围,现在我想知道如何获取单词。

var match = this.removeRange(0, this.indexOf(phrase, 0, true))
match = match.removeRange(phrase.length, match.length)

0 个答案:

没有答案