Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 77593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:51:17+00:00 2026-05-10T20:51:17+00:00

Are there any libraries out there for Java that will accept two strings, and

  • 0

Are there any libraries out there for Java that will accept two strings, and return a string with formatted output as per the *nix diff command?

e.g. feed in

test 1,2,3,4 test 5,6,7,8 test 9,10,11,12 test 13,14,15,16 

and

test 1,2,3,4 test 5,6,7,8 test 9,10,11,12,13 test 13,14,15,16 

as input, and it would give you

test 1,2,3,4                                                    test 1,2,3,4 test 5,6,7,8                                                    test 5,6,7,8 test 9,10,11,12                                               | test 9,10,11,12,13 test 13,14,15,16                                                test 13,14,15,16 

Exactly the same as if I had passed the files to diff -y expected actual

I found this question, and it gives some good advice on general libraries for giving you programmatic output, but I’m wanting the straight string results.

I could call diff directly as a system call, but this particular app will be running on unix and windows and I can’t be sure that the environment will actually have diff available.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:51:17+00:00Added an answer on May 10, 2026 at 8:51 pm

    I ended up rolling my own. Not sure if it’s the best implementation, and it’s ugly as hell, but it passes against test input.

    It uses java-diff to do the heavy diff lifting (any apache commons StrBuilder and StringUtils instead of stock Java StringBuilder)

    public static String diffSideBySide(String fromStr, String toStr){     // this is equivalent of running unix diff -y command     // not pretty, but it works. Feel free to refactor against unit test.     String[] fromLines = fromStr.split('\n');     String[] toLines = toStr.split('\n');     List<Difference> diffs = (new Diff(fromLines, toLines)).diff();      int padding = 3;     int maxStrWidth = Math.max(maxLength(fromLines), maxLength(toLines)) + padding;      StrBuilder diffOut = new StrBuilder();     diffOut.setNewLineText('\n');     int fromLineNum = 0;     int toLineNum = 0;     for(Difference diff : diffs) {         int delStart = diff.getDeletedStart();         int delEnd = diff.getDeletedEnd();         int addStart = diff.getAddedStart();         int addEnd = diff.getAddedEnd();          boolean isAdd = (delEnd == Difference.NONE && addEnd != Difference.NONE);         boolean isDel = (addEnd == Difference.NONE && delEnd != Difference.NONE);         boolean isMod = (delEnd != Difference.NONE && addEnd != Difference.NONE);          //write out unchanged lines between diffs         while(true) {             String left = '';             String right = '';             if (fromLineNum < (delStart)){                 left = fromLines[fromLineNum];                 fromLineNum++;             }             if (toLineNum < (addStart)) {                 right = toLines[toLineNum];                 toLineNum++;             }             diffOut.append(StringUtils.rightPad(left, maxStrWidth));             diffOut.append('  '); // no operator to display             diffOut.appendln(right);              if( (fromLineNum == (delStart)) && (toLineNum == (addStart))) {                 break;             }         }          if (isDel) {             //write out a deletion             for(int i=delStart; i <= delEnd; i++) {                 diffOut.append(StringUtils.rightPad(fromLines[i], maxStrWidth));                 diffOut.appendln('<');             }             fromLineNum = delEnd + 1;         } else if (isAdd) {             //write out an addition             for(int i=addStart; i <= addEnd; i++) {                 diffOut.append(StringUtils.rightPad('', maxStrWidth));                 diffOut.append('> ');                 diffOut.appendln(toLines[i]);             }             toLineNum = addEnd + 1;          } else if (isMod) {             // write out a modification             while(true){                 String left = '';                 String right = '';                 if (fromLineNum <= (delEnd)){                     left = fromLines[fromLineNum];                     fromLineNum++;                 }                 if (toLineNum <= (addEnd)) {                     right = toLines[toLineNum];                     toLineNum++;                 }                 diffOut.append(StringUtils.rightPad(left, maxStrWidth));                 diffOut.append('| ');                 diffOut.appendln(right);                  if( (fromLineNum > (delEnd)) && (toLineNum > (addEnd))) {                     break;                 }             }         }      }      //we've finished displaying the diffs, now we just need to run out all the remaining unchanged lines     while(true) {         String left = '';         String right = '';         if (fromLineNum < (fromLines.length)){             left = fromLines[fromLineNum];             fromLineNum++;         }         if (toLineNum < (toLines.length)) {             right = toLines[toLineNum];             toLineNum++;         }         diffOut.append(StringUtils.rightPad(left, maxStrWidth));         diffOut.append('  '); // no operator to display         diffOut.appendln(right);          if( (fromLineNum == (fromLines.length)) && (toLineNum == (toLines.length))) {             break;         }     }      return diffOut.toString(); }  private static int maxLength(String[] fromLines) {     int maxLength = 0;      for (int i = 0; i < fromLines.length; i++) {         if (fromLines[i].length() > maxLength) {             maxLength = fromLines[i].length();         }     }     return maxLength; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 221k
  • Answers 221k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The button's CanExecute is only called when something happens in… May 13, 2026 at 12:06 am
  • Editorial Team
    Editorial Team added an answer You might approach it like this: First, establish a wildcard… May 13, 2026 at 12:06 am
  • Editorial Team
    Editorial Team added an answer Just to go with Chuck's answer & Chris Smiths' comment,… May 13, 2026 at 12:06 am

Related Questions

Anyone who's tried to study mathematics using online resources will have come across these
Looking to develop server-side application that will process documents. The source documents are mostly
For context, we are storing most of our data as JSON strings. This works
Sorry for being somewhat vague but so is the project I'm leading now. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.