Monday 21, May 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    About Us    |    Contact Us

  Compare two strings using java

Here is a java program to compare two strings. It will return the position where they start to differ.
Cut & Paste Script
public class Main {

  public static String difference(String str1, String str2) {
      if (str1 == null) {
          return str2;
      }
      if (str2 == null) {
          return str1;
      }
      int at = indexOfDifference(str1, str2);
      if (at == -1) {
          return "";
      }
      return str2.substring(at);
  }
 
  public static int indexOfDifference(String str1, String str2) {
      if (str1 == str2) {
          return -1;
      }
      if (str1 == null || str2 == null) {
          return 0;
      }
      int i;
      for (i = 0; i < str1.length() && i < str2.length(); ++i) {
          if (str1.charAt(i) != str2.charAt(i)) {
              break;
          }
      }
      if (i < str2.length() || i < str1.length()) {
          return i;
      }
      return -1;
  }

}
 
   
     RELATED SCRIPTS
 
Reading file using java
Here is an example of reading text files in java....more »
Category : Java Programs Hits : 615
   
Making Beep sound using java
We are making Beep sound using java....more »
Category : Java Programs Hits : 653
   
Writing file using java
Here is a java code to write file....more »
Category : Java Programs Hits : 597
   
Handling exceptions in java
Here is a java program to handle an exception....more »
Category : Java Programs Hits : 500
   
   
SCRIPT CATEGORIES
 
  C Programs
C Programs scripts
  C++ Programs
C++ Programs scripts
  Java Programs
Java Programs scripts
  JavaScript Examples
JavaScript Examples scripts
  PHP Examples
PHP Examples scripts
  VBScript Examples
VBScript Examples scripts
 
 
 
POPULAR E-BOOKS
 
Download Pajama Paycheck Ebook Pajama Paycheck
   
Download MySQL Essentials  Ebook MySQL Essentials
   
Download Introduction To Programming With Java  Ebook Introduction To Programming With Java
   
Download Viral Marketing Secrets Ebook Viral Marketing Secrets
   
Download Design of VLSI Systems Ebook Design of VLSI Systems
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz