Difference Between Two String

The following JAVA PROGRAM calculates the difference between two strings provided the initial contents in both the strings remain the same.


The logic behind this program is to consider the lengths of the two strings.

If the first string is having larger length, then it is divided to get the difference and same goes for the second string.


Here is the sample code snippet.

package samples;

public class DifferenceBetweenTwoStrings {

public static void main(String[] args) {

String one = “this is first string”;

String two = “this is first string with some extra content”;

if (one.length() > two.length()) {

System.out.println(one.substring(two.length() – 1));

} else if (two.length() > one.length()) {

System.out.println(two.substring(one.length() – 1));

} else {

System.out.println(“something went wrong”);



Published by milindjagre

I founded my blog www.milindjagre.co four years ago and am currently working as a Data Scientist Analyst at the Ford Motor Company. I graduated from the University of Connecticut pursuing Master of Science in Business Analytics and Project Management. I am working hard and learning a lot of new things in the field of Data Science. I am a strong believer of constant and directional efforts keeping the teamwork at the highest priority. Please reach out to me at milindjagre@gmail.com for further information. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: