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

  • Home
  • SEARCH
  • 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 63k
  • Answers 63k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Honestly I would ditch the Sprite rendering object and switch… May 11, 2026 at 10:30 am
  • added an answer You can try to use Remotesoft Salamander. It does exactly… May 11, 2026 at 10:30 am
  • added an answer Helpers are mostly used for complex output-related tasks, like making… May 11, 2026 at 10:30 am

Related Questions

Are there any libraries out there for Java that will accept two strings, and
Are there any libraries out there (preferably a self contained Text Edit Control) for
Are there any UPS WorldShip integration APIS or Libraries out there for .net? I've
Are there any publicly available libraries or APIs out there on Ubuntu that allow
Are there any libraries, pieces of code or suchlike that'll let me play ZX
Are there any libraries (3rd party or built-in) in PHP to calculate text diffs?
Are there any libraries or resources available for parsing/reading an archived eventlogs?
Are there any libraries or guides for how to read and parse binary data
Are there any libraries that can take a few digital pictures of an object
Can audio being recorded be compressed on the fly? or are there any libraries

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.