Get Difference Between Two Strings in Java

This tutorial will teach you how to create a program that will get the difference between two inputted strings in java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of StringDiff.java. 2. Import the io package library:
  1. import java.io.*;
3. Now, we will create a string method named diff which has two parameters namely str1 and str2.
  1.         public static String diff(String str1, String str2) {
  2.     int index = str1.lastIndexOf(str2);
  3.     if (index > -1) {
  4.       return str1.substring(str2.length());
  5.     }
  6.     return str1;
  7.         }
As what you have seen the code above, we get the last index of str2 to be passed on the value of str1. Then we get the substring of str1 as it has the length of str2. Meaning, if we have inputted a string on str1 and input the same string in str2 then there is no difference. Thus, the remaining characters that does not inputted in str2 will be the answer or to be displayed by this method. 4. We will initialize variables in our Main, variable in as our BufferedReader for inputting, string1 and string2 as String.
  1.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  2.         String string1,string2;
  3.        
Then we will get input from the user.
  1.         System.out.print("Enter first string:");
  2.         string1 = in.readLine();
  3.         System.out.print("Enter second string:");
  4.         string2 = in.readLine();
Now, display the output and have to declare the method diff.
  1.         System.out.print("Output:" + diff(string1,string2));
Output: output Here's the full code of this tutorial:
  1. import java.io.*;
  2. public class StringDiff {
  3.        
  4.  
  5.         public static void main(String args[]) throws IOException{
  6.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  7.         String string1,string2;
  8.        
  9.         System.out.print("Enter first string:");
  10.         string1 = in.readLine();
  11.         System.out.print("Enter second string:");
  12.         string2 = in.readLine();
  13.        
  14.         System.out.print("Output:" + diff(string1,string2));
  15.          }
  16.          
  17.         public static String diff(String str1, String str2) {
  18.     int index = str1.lastIndexOf(str2);
  19.     if (index > -1) {
  20.       return str1.substring(str2.length());
  21.     }
  22.     return str1;
  23.         }
  24. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment