Home > Back-end >  String comparison size problem
String comparison size problem

Time:10-30

String 1.0.1.8 how to compare this type size
Eg: less than 1.0.1.10 1.0.1.8 expectations

CodePudding user response:

Put the string through the split for network stations, and then according to the length of the array and the index comparison respectively, we have to do is
Such as transformation of 1.0.1.8 array for a
A length of 4
A [0]=1
A [1]=0
A [2]=1
A [3]=8
1.0.1.10 into array b
Length for 4 b
B [0]=1
B [1]=0
B [2]=1
B [3]=10

Then find two minimum length of the array in the array, according to the length of the array index
Int iShortLength=a. ength<=b.l ength? A. ength: b.l ength;
for (int i=0; I & lt; IShortLength; I++) {
If (a [I] & gt; [I]) {b
System. The out. Println (" first large number ");
break;
} else if (a [I] <[I]) {b
System. The out. Println (" the second number ");
break;
}

If (I==iShortLength - 1) {
System. The out. Println (" two as large number ");
}


}

for

CodePudding user response:

reference 1st floor pengbo19851985 response:
to string by the split for network stations, then according to the length of the array and the index comparison respectively, we have to do is
Such as transformation of 1.0.1.8 array for a
A length of 4
A [0]=1
A [1]=0
A [2]=1
A [3]=8
1.0.1.10 into array b
Length for 4 b
B [0]=1
B [1]=0
B [2]=1
B [3]=10

Then find two minimum length of the array in the array, according to the length of the array index
Int iShortLength=a. ength<=b.l ength? A. ength: b.l ength;
for (int i=0; I & lt; IShortLength; I++) {
If (a [I] & gt; [I]) {b
System. The out. Println (" first large number ");
break;
} else if (a [I] <[I]) {b
System. The out. Println (" the second number ");
break;
}

If (I==iShortLength - 1) {
System. The out. Println (" two as large number ");
}


}

For


This isn't too much trouble, is there any easier way to

CodePudding user response:

Remove the decimal point directly, and then converted to int can't compare

CodePudding user response:

Is this IP address? IP address can be converted into an unsigned integer, and then you can compare the size,
  • Related