-1

I have to write the condition in Java, which will check whether the variable ends with the characters '-1'. If so let assign another variable value of '1', if not '2'.

I do not know Java, but I wrote something like this...

var adres = 'something@something.com-1';

if ( adres.contains("-1") ) {
  { var someVariable = '1' };
} else {
  { var someVariable = '2' };
} 

The script does not work. In addition, the "contains" is not the best solution, because the address might look like this: 'something@something.com-12'.

I will be grateful for your help.

Lech
  • 61
  • 2

1 Answers1

-1
if(adres.substring( adres.length() - 2, adres.length() ).equals( "-1" ) )

in Java (you choose java as tag ^^)

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
Marcel
  • 1,509
  • 1
  • 17
  • 39
  • 5
    1. format your code properly. 2. why would someone use substring + equals if he could just use `endsWith(...)`? – ParkerHalo Feb 19 '16 at 11:08
  • cause i didnt know that there is endsWith(), i just told him ONE way to do it, the ONE that i know, but thanks for telling – Marcel Feb 19 '16 at 11:09