kotlin 求两个字符串的差

zi8p0yeb  于 2023-04-07  发布在  Kotlin
关注(0)|答案(3)|浏览(259)

例如,我有两个字符串:
val string1 = "Nature is so beautiful"
val string2 = "Nature is very beautiful"
所以我想打印两个字符串:“so”之前的字符串和“very”编辑的字符串。我如何才能实现这一点,有人能帮助我吗?
我一直在努力

@Composable
    fun DifferenceText(oldText: String, currentText: String) {
        // Split both strings into lists of words
        val oldWords = oldText.split(" ")
        val currentWords = currentText.split(" ")

        // Compare each word in the old and current lists
        Column(
            modifier = Modifier.fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            for (i in oldWords.indices) {
                if (oldWords[i] != currentWords[i]) {
                    println(currentWords[i])
                    Text(text = currentWords[i])
                    break
                }
            }
        }
        }
    }
unguejic

unguejic1#

val string1 = "Nature is so beautiful"
    val string2 = "Nature is very beautiful"

    val listOfStringOne = string1.split(" ")
    val listOfStringTwo= string2.split(" ")

    var previousString = ""
    var editedString = ""

    for (i in listOfStringOne.indices){
        if (listOfStringOne[i] != listOfStringTwo[i]){  ->  compare two string and when match is not found then if condition execute and both of the string save in the declared variable. <br>
            previousString = listOfStringOne[i]  ->  String from first list (Previous String)  
            editedString = listOfStringTwo[i]  ->  String from second list (Edited String)
        }
    }

首先,我们需要使用split函数来列出句子中的单词。
我们需要比较这两个列表。

如果第二个字符串中的任何单词与第一个字符串不同,则我们可以保存前一个字符串中的第一个字符串值列表和编辑后的字符串中的第二个字符串列表

uqzxnwby

uqzxnwby2#

尝试以下代码来检查两个字符串中的单词是否不同:

val string1 = "Nature is so beautiful"
val string2 = "Nature is very beautiful"

//Separate both the strings into words
val stringOneWords = string1.split(" ")
val stringTwoWords = string2.split(" ")

//Iterate over each word in one string
for (i in stringOneWords.indices){
    //Check if a word in one string is not there in second string
    if (stringOneWords[i] != stringTwoWords[i]){
        //If any word is different then print the word from both strings  
        println(stringOneWords[i])
        println(stringTwoWords[i])
    }
}

在上面的代码中,我们首先将字符串分成单词。然后我们迭代一个字符串中的每个单词,并检查该单词是否存在于另一个字符串中。如果该单词不在另一个字符串中,那么我们打印两个字符串中的单词。

2uluyalo

2uluyalo3#

我会做一些像

val map = oldText.split(" ").map { it to 1 }.toMap().toMutableMap()
val newTextList = newText.split(" ")
val result = mutableListOf<String>()

newTextList.forEach {
    if (map.contains(it)) {
        map[it] = map[it]?.let { it + 1 } ?: 0
    } else {
        println(it) // will print "very"
        result.add(it)
    }
}

map.forEach {
    if (it.value == 1) {
        println(it.key) // will print "so"
        result.add(it)
    }
}

result.forEach {
    Text(text = it)
}

上面的解决方案可以让你打印所有唯一的单词。即使句子中的单词数量不同。
假设只有一个字的差异,可以使用StringUtils.difference

val a = StringUtils.difference(oldText, newText) // "very beautiful"
val b = StringUtils.difference(newText, oldText) // "so beautiful"
val c = StringUtils.difference(a.reversed(), b.reversed()).reversed() // "so"
val d = StringUtils.difference(b.reversed(), a.reversed()).reversed() // "very"

相关问题