Skip to content

Instantly share code, notes, and snippets.

@androidcodehunter
Created June 12, 2019 04:53
Show Gist options
  • Save androidcodehunter/cdd43daac61b02968f5603a6aab2c7fb to your computer and use it in GitHub Desktop.
Save androidcodehunter/cdd43daac61b02968f5603a6aab2c7fb to your computer and use it in GitHub Desktop.
First method accept a string, and will return true if the first letter of that string is found in the range [A-Z]. Second method is a test method.
/**
* Check first character of a String is a letter [A-Z]. If first character is letter, return true otherwise return false.
*
* @param value input value as String
* @return return true if the first character is in the Range [A-Z]
*/
private boolean isFirstCharacterLetter(String value){
if (value == null || value.isEmpty()){
return false;
}
String input = value.trim();
if (input.isEmpty()){
return false;
}
return Character.isLetter(input.charAt(0));
}
@Test
public void firstCharacterLetterTest(){
boolean result = isFirstCharacterLetter("Sharif");
Assert.assertEquals(result, true);
/*Input is null*/
result = isFirstCharacterLetter(null);
Assert.assertEquals(result, false);
/*Input is only number*/
result = isFirstCharacterLetter("122");
Assert.assertEquals(result, false);
/*Input is empty*/
result = isFirstCharacterLetter(" ");
Assert.assertEquals(result, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment