Pregunta

I'm trying to check if a string alphabetically fits between two other strings. At the moment i'm doing it like this:

if(_subSize.compareTo("440/65") > 0 && "600/65".compareTo(_subSize) < 0){
    //fits in
} else { // fits not in between }

But when I have as _subSize = "440/65"; the if statement gets in the else. I've fixed that like this:

if(_subSize.compareTo("440/65") >= 0 && "600/65".compareTo(_subSize) <= 0){
    //fits in
} else { // fits not in between }

now the first statement is true. Yet the 2nd statement is false. How to get the 2nd statement to be true aswell? as "440/65" alphabetically comes before "600/65" so technically it should be lower or equals to 0. But somehow it's not working.

And i need the strings to be as string. They are no digits or something.

¿Fue útil?

Solución

The 2nd .compareTo() is inverted related to the first one. You just need to have both to compare from the same perspective as follows:

if(_subSize.compareTo("440/65") >= 0 && _subSize.compareTo("600/65") <= 0){
    //fits in
} else { // fits not in between }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top