在Scala中提取两个字符串之间的差异,交集和不同字符

Given two strings, we have to find the difference, intersections, and distinct characters between two strings.

给定两个字符串,我们必须找到两个字符串之间的差异,交点和不同字符。

两根弦之间的区别 (Difference between two strings)

The difference between the two strings gives the characters that are present in the first string and not in second.

两个字符串之间的差异给出了出现在第一个字符串中而不是第二个字符串中的字符。

Syntax:

句法:

    str1 diff str2

Returns a string that contains all those characters that are in str1 but not in str2.

返回一个字符串,其中包含所有在str1中但不在str2中的字符

Example1:

范例1:

object MyClass {

    def main(args: Array[String]) {
        val str1 = "BMW S1000 RR"
        val str2 = "BMW S1000 XR"
        println("String 1 = " + str1)
        println("String 2 = " + str2)
        val difference = str1 diff str2
        println("The difference between str1 and str2 is "+ difference)
    }
}

Output

输出量

String 1 = BMW S1000 RR
String 2 = BMW S1000 XR
The difference between str1 and str2 is R

Explanation:

说明:

Here, we have initialized two strings str1 = "BMW S1000 RR" and str2 = "BMW S1000 XR". And then we will find the difference between str1 and str2 which will return the character of difference between them which is R.

在这里,我们初始化了两个字符串str1 =“ BMW S1000 RR”str2 =“ BMW S1000 XR” 。 然后我们将找到str1str2之间的差异,该差异将返回它们之间的差异字符R。

Example 2:

范例2:

object MyClass {

    def main(args: Array[String]) {
        val str1 = "BMW S1000 RR"
        val str2 = "BMW S1000 XR"
        println("String 1 = " + str1)
        println("String 2 = " + str2)
        val difference = str2 diff str1
        println("The difference between str2 and str1 is "+ difference)
    }
}

Output

输出量

String 1 = BMW S1000 RR
String 2 = BMW S1000 XR
The difference between str2 and str1 is X

Explanation:

说明:

Here, we have initialized two strings str1 = "BMW S1000 RR" and str2 = "BMW S1000 XR". And then we will find the difference between str2 and str1 which will return the character of difference between them which is X.

在这里,我们初始化了两个字符串str1 =“ BMW S1000 RR”str2 =“ BMW S1000 XR” 。 然后我们将找到str2str1之间的差异,该差异将返回它们之间的差异字符X。

两根弦的交点 (Intersection of two strings)

The intersection of two strings gives the characters that are present in both the strings.

两个字符串的交集给出了两个字符串中都存在的字符。

Syntax:

句法:

    str1 intersect str2

Returns a string that contains a character that are present in both the strings.

返回一个字符串,其中包含两个字符串中都存在的字符。

Example:

例:

object MyClass {

    def main(args: Array[String]) {
        val str1 = "BMW S1000 RR"
        val str2 = "BMW S1000 XR"
        println("String 1 = " + str1)
        println("String 2 = " + str2)
        val intersect = str1 intersect str2
        println("The intersection of str1 and str2 is " + intersect)
    }
}

Output

输出量

String 1 = BMW S1000 RR
String 2 = BMW S1000 XR
The intersection of str1 and str2 is BMW S1000 R

Explanation:

说明:

Here, we have initialized two strings str1 = "BMW S1000 RR" and str2 = "BMW S1000 XR". And then we will find the intersection of both the strings which will return the character of intersection between them which is BMW S1000 R.

在这里,我们初始化了两个字符串str1 =“ BMW S1000 RR”str2 =“ BMW S1000 XR” 。 然后我们将找到两个字符串的交点,这将返回它们之间的交集字符,即BMW S1000 R。

字符串中的不同字符 (Distinct characters in the string)

The distinct finds the character that are unique and also the first occurrence of duplicate characters.

唯一字符找到唯一的字符,并且首次发现重复的字符。

Syntax:

句法:

    string.distinct

Returns the elements that are unique in the string. And the first occurrence of all duplicate characters.

返回字符串中唯一的元素。 并且第一次出现所有重复字符。

Example:

例:

object MyClass {
    def main(args: Array[String]) {
        val string = "includehelp"
        println("String = " + string)
        val distinct = string.distinct 
        println("The distinct elements are " + distinct)
    }
}

Output

输出量

String = includehelp
The distinct elements are includehp


翻译自: https://www.includehelp.com/scala/extracting-difference-intersection-and-distinct-characters-between-two-strings-in-scala.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值