I would like to declare a String
for a country variable in Java
which would only take 2 characters such as IN
, DE
etc. I am a bit confused so can someone please help me?
If I try to assign a value something like this then it should work:
country = "IN"
but if I try to assign a value country = "IND"
then it should throw an error.
I am a bit familiar with regex
but wanted to confirm if there is any built-in String method that would do this.
public class App {
public static void main(String[] args) {
String country = "";
//char[] array = new char[2];
//String country = new String(array);
// String[] country = new String[2];
}
}