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” 。 然后我们将找到str1和str2之间的差异,该差异将返回它们之间的差异字符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” 。 然后我们将找到str2和str1之间的差异,该差异将返回它们之间的差异字符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