Home »
Scala »
Scala Programs
Extracting difference, intersection, and distinct characters between two strings in Scala
Here, we will learn to perform different operations like difference, intersection, and distinct characters between two strings in the Scala programming language.
Submitted by Shivang Yadav, on May 05, 2020 [Last updated : March 10, 2023]
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.
Example 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.
Example 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.
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.
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
Scala String Programs »