Python的字符串模块有一些方便的操作,它们将返回某些字符集,例如所有大写字符.Java有什么类似的东西吗?
http://docs.python.org/2/library/string.html
string.ascii_lowercase
Run Code Online (Sandbox Code Playgroud)
小写字母'abcdefghijklmnopqrstuvwxyz'.
string.ascii_uppercase
Run Code Online (Sandbox Code Playgroud)
大写字母'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
string.digits
Run Code Online (Sandbox Code Playgroud)
字符串'0123456789'.
string.punctuation
Run Code Online (Sandbox Code Playgroud)
ASCII字符串,在C语言环境中被视为标点字符.
你可以只做 toLowerCase() 和 toUpperCase()
String abc = aBc;
abc.toLowerCase(); // abc
abc.toUpperCase(); // ABC
Run Code Online (Sandbox Code Playgroud)
编辑:我误读了OP。
OP 不想转换字符串,而是使用 Java 获取所有大写/小写、标点符号和数字的集合
public static char[] getUpper() {
char[] res = new char[26];
for(int i = 0; i <= 26; i++) {
res[i] = 'A' + i;
}
return res;
}
// Or just do getUpper().toLowerCase();
public static char[] getUpper() {
char[] res = new char[26];
for(int i = 0; i <= 26; i++) {
res[i] = 'a' + i;
}
return res;
}
public static char[] getUpper() {
char[] res = new char[10];
for(int i = 0; i <= 10; i++) {
res[i] = '0' + i;
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
对于标点符号我真的不知道
您可以只使用一些直接返回的方法,因为您知道输出应该是什么。
public static String getUpper() {
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
public static String getLower() {
return "abcdefghijklmnopqrstuvwxyz"
}
public static String getDigits() {
return "0123456789"
}
public static String getPunctuation() {
return ".," // Don't really know what this should return
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
842 次 |
| 最近记录: |