字符大小写转换的方式

1. 类型转换

public static void main(String[] args) {
char a = 'a';
int n = (int)a -32;
System.out.println(n);
char A = (char)n;
System.out.println(A);
}

2. 内置方法

public static void main(String[] args) {
char a = 'a';
String s = a+"";
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
}

方式不止这两种,字符型使用1更简便。

​2309. 兼具大小写的最好英文字母​

class Solution {
public String greatestLetter(String s) {
char temp=' ';
Set<Integer> set = new HashSet<Integer>();

for(int i = 0;i<s.length();i++){
char c =s.charAt(i);
int n = (int)c;
if(set.contains(n-32)){
if(temp<n-32) temp =(char)(n-32);
}else if(set.contains(n+32)){
if(temp<n) temp =(char)n;
}
set.add(n);
}
if(temp==' ') return "";

return temp+"";

}
}

另外两种字符串方法。

public static void main(String[] args) {
String s = "0123456789";

System.out.println(s.substring(0,1));//0
System.out.println(s.split("5")[1]);//6789

}