Q:
Extracting difference, intersection, and distinct characters between two strings in Scala
belongs to collection: Scala String Programs
Scala String Programs
- How to determine if a string contains a regular expression in Scala?
- Scala program to split string
- Scala program to convert string to integer
- How to count the number of characters in a string in Scala?
- How to create a range of characters in Scala?
- How to left-trim and right-trim strings in Scala?
- How to get date, month and year as string or number in Scala?
- How to replace a regular expression pattern in a string in Scala?
- Extracting difference, intersection, and distinct characters between two strings in Scala
- How to convert a string to byte array in Scala?
- How to convert byte array to string in Scala?
- How to convert a string to date in Scala?
- How to convert hex string to int in Scala?
- How to convert hex string to long in Scala?
- How to convert Int to Double in Scala?
- Scala program to compare strings using equal to (==) operator
- Scala program to compare strings using equals() method
- Scala program to compare strings using compareTo() method
- Scala program to compare strings using equalsIgnoreCase() method
- Scala program to concatenate strings using the plus (+) operator
- Scala program to concatenate strings using the concat() method
- Scala program to extract the substring from a string
- Scala program to find the length of the string
- Scala program to get characters from the string
- Scala program to check the string starts with a specified substring
- Scala program to check the string ends with specified substring
- Scala program to get bytes array from string using getBytes() method
- Scala program to copy the content of string into a character array
- Scala program to copy the content of string into a character array
- Scala program to convert the string into a character array
- Scala program to convert an integer number into a string
- Scala program to convert a floating-point number into a string
- Scala program to get the hash code of the string
- Scala program to get the index of the first occurrence of the specified substring
- Scala program to get the index of the last occurrence of the specified substring
- Scala program to replace the character with another character in the string
- Scala program to demonstrate the replaceAll() method
- Scala program to demonstrate the replaceFirst() method
- Scala program to split the string based on the given separator
- Scala program to demonstrate the subSequence() method
- Scala program to convert the string into lowercase
- Scala program to convert the string into uppercase
- Scala program to trim a string
- Scala program to concatenate string using \'s\' method of string interpolation
- Scala program to concatenate formatted string using \'f\' method of string interpolation
- Scala program to print raw string using the raw method of string interpolation
- Scala program to create a string using StringBuilder class
- Scala program to append a character to an immutable string using the \'+=\' operator
- Scala program to append a string to an immutable string using the \'++=\' operator
- Scala program to append a number to an immutable string using append() method
- Scala program to clear the content of StringBuilder string
- Scala program to delete a substring from StringBuilder string
- Scala program to insert a substring into StringBuilder string
- Scala program to replace a substring into StringBuilder string
- Scala program to get the length of StringBuilder string
- Scala program to get characters from StringBuilder string
- Scala program to check whether a string is empty or not
- Scala program to check nullable string
- Scala program to create a string using StringBuffer class
- Scala program to demonstrate the append() method with StringBuffer class
- Scala program to delete a substring from a string created using StringBuffer
- Scala program to insert a substring into string created using StringBuffer
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:
Returns a string that contains all those characters that are in str1 but not in str2.
Example1:
Output
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:
Output
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:
Returns a string that contains a character that are present in both the strings.
Example:
Output
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:
Returns the elements that are unique in the string. And the first occurrence of all duplicate characters.
Example:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer