Java怎么判断字符串首个是否是数字
时间: 2023-09-07 18:17:40 浏览: 119
Java中可以通过字符串的charAt()方法获取指定位置的字符,从而判断字符串的首个字符是否是数字。
下面是一个示例代码:
```java
public static boolean isFirstCharDigit(String str) {
if (str == null || str.isEmpty()) {
return false;
}
char firstChar = str.charAt(0);
return Character.isDigit(firstChar);
}
```
该方法接受一个字符串参数,如果字符串为空或长度为0,则返回false;否则获取字符串的第一个字符,使用Character类中的isDigit()方法判断该字符是否是数字,如果是则返回true,否则返回false。
相关问题
java字符串工具类示例源代码
### 回答1:
以下是一个 Java 字符串工具类的示例源代码:
```
public class StringUtils {
/**
* 判断字符串是否为空
* @param str 待判断的字符串
* @return true/false
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == ;
}
/**
* 判断字符串是否不为空
* @param str 待判断的字符串
* @return true/false
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* 判断字符串是否为数字
* @param str 待判断的字符串
* @return true/false
*/
public static boolean isNumeric(String str) {
if (isEmpty(str)) {
return false;
}
for (int i = ; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* 判断字符串是否为字母
* @param str 待判断的字符串
* @return true/false
*/
public static boolean isLetter(String str) {
if (isEmpty(str)) {
return false;
}
for (int i = ; i < str.length(); i++) {
if (!Character.isLetter(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* 判断字符串是否为汉字
* @param str 待判断的字符串
* @return true/false
*/
public static boolean isChinese(String str) {
if (isEmpty(str)) {
return false;
}
for (int i = ; i < str.length(); i++) {
if (str.charAt(i) < x4e00 || str.charAt(i) > x9fa5) {
return false;
}
}
return true;
}
/**
* 将字符串转换为大写
* @param str 待转换的字符串
* @return 转换后的字符串
*/
public static String toUpperCase(String str) {
if (isEmpty(str)) {
return str;
}
return str.toUpperCase();
}
/**
* 将字符串转换为小写
* @param str 待转换的字符串
* @return 转换后的字符串
*/
public static String toLowerCase(String str) {
if (isEmpty(str)) {
return str;
}
return str.toLowerCase();
}
/**
* 将字符串首字母大写
* @param str 待转换的字符串
* @return 转换后的字符串
*/
public static String capitalize(String str) {
if (isEmpty(str)) {
return str;
}
char firstChar = Character.toUpperCase(str.charAt());
if (str.length() == 1) {
return String.valueOf(firstChar);
}
return firstChar + str.substring(1);
}
/**
* 将字符串首字母小写
* @param str 待转换的字符串
* @return 转换后的字符串
*/
public static String uncapitalize(String str) {
if (isEmpty(str)) {
return str;
}
char firstChar = Character.toLowerCase(str.charAt());
if (str.length() == 1) {
return String.valueOf(firstChar);
}
return firstChar + str.substring(1);
}
}
```
### 回答2:
Java字符串工具类示例源代码如下:
```
public class StringUtil {
/**
* 判断字符串是否为空或null
* @param str 要判断的字符串
* @return 若为空或null,返回true;否则返回false
*/
public static boolean isEmpty(String str) {
return str == null || "".equals(str);
}
/**
* 判断字符串是否为非空
* @param str 要判断的字符串
* @return 若不为空,返回true;否则返回false
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* 去掉字符串两端的空白字符
* @param str 要处理的字符串
* @return 去掉空白字符的新字符串
*/
public static String trim(String str) {
return str == null ? null : str.trim();
}
/**
* 比较两个字符串是否相等,忽略大小写
* @param str1 字符串1
* @param str2 字符串2
* @return 若两个字符串相等,返回true;否则返回false
*/
public static boolean equalsIgnoreCase(String str1, String str2) {
return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
}
/**
* 反转字符串
* @param str 要反转的字符串
* @return 反转后的字符串
*/
public static String reverse(String str) {
return str == null ? null : new StringBuilder(str).reverse().toString();
}
}
```
以上是一个简单的Java字符串工具类示例,包含了常见的字符串操作方法,如判断字符串是否为空、去除空白字符、比较字符串是否相等(忽略大小写)、反转字符串等。可以在实际开发中进行改进和扩展,以满足不同的需求。
### 回答3:
下面是一个简单的Java字符串工具类示例源代码:
```java
public class StringUtils {
/**
* 判断字符串是否为空
* @param str 要判断的字符串
* @return true表示字符串为空,false表示字符串不为空
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
/**
* 反转字符串
* @param str 要反转的字符串
* @return 反转后的字符串
*/
public static String reverse(String str) {
if (isEmpty(str)) {
return str;
}
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
/**
* 将字符串转换为大写
* @param str 要转换的字符串
* @return 转换后的字符串
*/
public static String toUpperCase(String str) {
if (isEmpty(str)) {
return str;
}
return str.toUpperCase();
}
/**
* 将字符串转换为小写
* @param str 要转换的字符串
* @return 转换后的字符串
*/
public static String toLowerCase(String str) {
if (isEmpty(str)) {
return str;
}
return str.toLowerCase();
}
}
```
以上是一个简单的Java字符串工具类示例,其中包含了判断字符串是否为空、反转字符串、将字符串转换为大写和小写等常用方法。你可以根据自己的需求修改或添加其他方法。
java8 将中文英文数字都有的一串字符首位保留其他位置加*
可以使用Java 8中的正则表达式和字符串操作来实现这个功能,以下是示例代码:
```java
public static String maskString(String str) {
if (str == null || str.isEmpty()) {
return str;
}
int len = str.length();
if (len == 1) {
return str;
}
char firstChar = str.charAt(0);
String mask = "*";
String regex = "[\u4e00-\u9fa5]"; // 匹配中文字符的正则表达式
if (Character.isLetterOrDigit(firstChar)) {
mask = "*";
} else if (Character.toString(firstChar).matches(regex)) {
mask = "**";
}
String maskedStr = Character.toString(firstChar) + str.substring(1).replaceAll(".", mask);
return maskedStr;
}
```
上述代码中,首先判断输入的字符串是否为空或只有一个字符,如果是则直接返回原字符串。然后获取第一个字符,判断其是否是字母或数字,如果是则用一个星号代替其他字符,否则用两个星号代替中文字符和其他字符。最后使用字符串操作将剩余字符替换为星号,返回处理后的字符串即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)