Char, lower/upper case

Hi!
Is there a way to check if a char is uppercase / lowercase other than:
(char+"").toLowerCase().charAt(0) == char ?

I saw this method


but it didn’t seem to work.
I tried using: println(a.isUpperCase(), isUpperCase(a), a.isUpperCase(int(a)), "etc");
but none worked.

What I need:

  • a way to check if the input char is upper case (true / false)
  • a way to set the CASE of a char (simple using strings, but is it wasteful?)
    ( setCase(char c) )
  • a way to invert the case of a char ( invertCase(char c) )
  • compare if char1 == CHAR1 (can use multiple functions)

Thank you!

1 Like

this is my code using strings:

void setup() {
  String a = "abAB";
  println(a, rem(a,1,2), isUpper("a"), invert('a'),invert('B'));
}
boolean isUpper(String c) {
  return c.equals(c.toUpperCase());
}
char invert(char c) {
  if(isUpper(c+"")) return (c+"").toLowerCase().charAt(0);
  else return (c+"").toUpperCase().charAt(0);  
}
char setCase(String input, boolean upperCase) {
  if(upperCase) return input.toUpperCase().charAt(0);
  else return input.toLowerCase().charAt(0);
}
String rem(String input, int start, int stop) {
  String output = "";
  for(int i = 0; i < input.length(); i++) if(i < start || i > stop) output += input.charAt(i);
  return output;
}
1 Like

Hello,

You can write your own custom routines.
I did this all the time in small embedded systems when a library just bloated the code.
Take a look at the ASCII table and you will see patterns that you can work with.

void draw() 
	{}

void keyPressed()
  {
  //if (key >= 0x61 && key <= 0x7A)
  if (key >= 'a' && key <= 'z')
    {
    println(key, "LowerCase");  
    println(char(key + 'A' - 'a'), "UpperCase"); 
    }
  }

References:

:)

2 Likes
3 Likes
void setup() {
  char myChar = 'a';
  
  boolean isLowerCase = Character.isLowerCase(myChar);
  println("is myChar lower case: " + isLowerCase);
  
  myChar = Character.toUpperCase(myChar);
  
  isLowerCase = Character.isLowerCase(myChar);
  println("is myChar lower case: " + isLowerCase);
}

edit: isUpperCase isLowerCase toLowerCase toUpperCase are static methods of the class Character and need to be called via it.

3 Likes