compare string integer java

Solutions on MaxInterview for compare string integer java by the best coders in the world

showing results for - "compare string integer java"
Lucas
26 Apr 2018
1/**
2    * Similar to compareTo method But compareTo doesn't return correct result for string+integer strings something like `A11` and `A9`
3*/
4
5private int newCompareTo(String comp1, String comp2) {
6  // If any value has 0 length it means other value is bigger
7  if (comp1.length() == 0) {
8    if (comp2.length() == 0) {
9      return 0;
10    }
11    return -1;
12  } else if (comp2.length() == 0) {
13    return 1;
14  }
15  if (TextUtils.isDigitsOnly(comp1)) {
16    int val1 = Integer.parseInt(comp1);
17    if (TextUtils.isDigitsOnly(comp2)) {
18      int val2 = Integer.parseInt(comp2);
19      return Integer.compare(val1, val2);
20    } else {
21      return comp1.compareTo(comp2);
22    }
23
24  } else {
25
26    int minVal = Math.min(comp1.length(), comp2.length()), sameCount = 0;
27
28    // Loop through two strings and check how many strings are same
29    for (int i = 0;i < minVal;i++) {
30      char leftVal = comp1.charAt(i), rightVal = comp2.charAt(i);
31      if (leftVal == rightVal) {
32        sameCount++;
33      } else {
34        break;
35      }
36    }
37    if (sameCount == 0) {
38      return comp1.compareTo(comp2);
39    } else {
40      String newStr1 = comp1.substring(sameCount), newStr2 = comp2.substring(sameCount);
41      if (TextUtils.isDigitsOnly(newStr1) && TextUtils.isDigitsOnly(newStr2)) {
42        return Integer.compare(Integer.parseInt(newStr1), Integer.parseInt(newStr2));
43      } else {
44        return comp1.compareTo(comp2);
45      }
46    }
47  }
48}
queries leading to this page
java compare with two intcan we compare a string with a string and integerjava compare string with inthow to compare integers in javajava comparing int values in string syntaxcompare numbers in javajava compareto fot intwhen int convert to string condition in javastring 5b0 5d to compare to a inthow to compare integer in javaint compare javastring integer compare javahow to compare string and int in javacompare string with intcompare int javacompare two java integercompare two integers in javajava int comparetocompareto method in java for integerscompare integer string ifjava comparing intscan i compare int and integer javaint comparison in javajava string compare value as string 7c 7c integercomparison between integers and string in javacompareto java for intcompareto string with integers javacompare integer java with 3d 3dhow do i compare all integers in java compare int and integer javajava compare numbers java int comparisoncompare to function integers javacan i compare int and string in javainteger compare function javahow to compare string and integer in javahow to compare two integer string in javanumber string comparison javacan you compare int to string in javacomparer integer strings javacompare 2 integers in javajava int with specific didgitscompare string and int value in javacompare int in javajava compare two integersjava compare 2 integerscompare integer and int javacompare numerical string javajava compareto integercompareto int javajava compare integerhow to compare a string and an integer in javahow to write compareto for integer javacompare int and integer in javastring compare with int in javacomparison in java with intjava compare intjava comparing two numberscompare 2 ints javajava compare string of intsjava compare to int compareto with integers javahow to compare strings in java that have numbersjava how to compare 2 integerscompare int and string javahow to compare string element and an integerstring and integer comaprisoncomparing integer to stringjava compare two number stringscompare integer strings javajava compare strings as numberscompare length of int javacomparing ints in javahow to compareto ints in javainteger string comparisonjava compare strings integersinteger compare javacompare two integer in javacomparing string numbers javajava comparing int and integercompare integer int javacompare int using javahow to compare numbers with strings javacomparing and int to a variable javajava compare two integerhow to compare ints in javahow to compare number string and integer in javajava compare int with intcompare string and int javajava compareintcomparing integers javastring to integer compare in javajava code for comparing two intscompare string integer javajava compare bidimensional int with stringhow to compare two numeric strings in javahow to compare string to int in javahow to compare ints javacompare int 5b 5d javacompare string of numbers javacompare number string in javainteger compare values javacompare integer javahow to compare two int in javajava comparator with string and inthow to compare integer value in javahow to compare string numbers in javajava 26 comparing intcomparing numbers inside a string javajava compare string to intjava compare int valueshow java compareto string digitjava compare integers equalityhow do i compare integers in javaint compare to javacan you compare string integers in javaint compareto javahow to compare two integersin javahow to compare two integers in javajava int compare tojava compare class for integerhow to compare integer and string in javahow to compare two int values in javacompare integer in javainteger comparision and string comparisioncompareto with strings that contain integers javainteger comparison in javacompare ints javacompare numeric string javajava integer comparecompare integers javacompare two ints javacompare number in javainteger compare in javacompareto with integers javahow to compare number in javahow to compare 4 integers in javacompare integers in javacompare two integer javaint compare javastring integer compare greater javajava compareto intscompare string to int in javacomparing int and integer javahow to compare string with integerjava integer comparecompareto two integer in javainteger compare javahow to compare int value of string in javaintegerformat javahow to compare two ints javacompare 3 int javacan you can compare a string to an integer in javacompare two integers javahow to compare integer values in javajava compareto intinteger compare in javacompare ints jahow to compare int in javainteger compare javajava compare int with integerjava string compare any numbercompare strings to integers javajava compare int numbers java compare intshow to compare numbers in javacompareto java intcompare to int javacomparing integers in javajava compare two intsjava compate strings integersjava comparing numbersjava compare integersjava comparing integerscompare two int numbers javacompare if int in javajava integer value comparisonhow to compare int with string in javahow to compare numbers javahow to compare to ints in javaint comparison javacompare string integer java