Get Difference Between Two Strings in Java
Submitted by donbermoy on Friday, December 26, 2014 - 21:04.
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:
3. Now, we will create a string method named diff which has two parameters namely str1 and str2.
Then we will get input from the user.
Output:
Here's the full code of this tutorial:
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
- import java.io.*;
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.
- String string1,string2;
Now, display the output and have to declare the method diff.
- import java.io.*;
- public class StringDiff {
- String string1,string2;
- string1 = in.readLine();
- string2 = in.readLine();
- }
- int index = str1.lastIndexOf(str2);
- if (index > -1) {
- return str1.substring(str2.length());
- }
- return str1;
- }
- }
Add new comment
- 136 views