Android 判断字符串是否相等

在Android中:
判断两个String是否相等不能直接用== 或!=,需要用equals()判断,若相等,则返回1

String a = "hello";
String b = "world";
if (a.equals(b) {
    System.out.println("不一致");
}else{
    System.out.println("一致");
}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android中提供了丰富的字符串操作方法,但是如果想要更加方便地操作字符串,我们可以自定义一个字符串工具类。下面是一个简单的字符串工具类示例: ```java public class StringUtils { /** * 判断字符串是否为空 * * @param str 字符串 * @return true为空,false为非空 */ public static boolean isEmpty(String str) { return str == null || str.length() == 0; } /** * 判断字符串是否为非空 * * @param str 字符串 * @return true为非空,false为空 */ public static boolean isNotEmpty(String str) { return !isEmpty(str); } /** * 判断字符串是否为空白 * * @param str 字符串 * @return true为空白,false为非空白 */ public static boolean isBlank(String str) { return str == null || str.trim().length() == 0; } /** * 判断字符串是否为非空白 * * @param str 字符串 * @return true为非空白,false为空白 */ public static boolean isNotBlank(String str) { return !isBlank(str); } /** * 截取字符串 * * @param str 字符串 * @param start 起始位置(包括) * @param end 结束位置(不包括) * @return 截取后的字符串 */ public static String substring(String str, int start, int end) { if (str == null) { return null; } if (end < 0) { end = str.length() + end; } if (start < 0) { start = str.length() + start; } if (end > str.length()) { end = str.length(); } if (start > end) { return ""; } if (start < 0) { start = 0; } if (end < 0) { end = 0; } return str.substring(start, end); } /** * 字符串替换 * * @param str 字符串 * @param oldString 要替换的字符串 * @param newString 替换成的字符串 * @return 替换后的字符串 */ public static String replace(String str, String oldString, String newString) { if (str == null) { return null; } int index = str.indexOf(oldString); if (index < 0) { return str; } StringBuilder sb = new StringBuilder(); int lastIndex = 0; while (index >= 0) { sb.append(str.substring(lastIndex, index)).append(newString); lastIndex = index + oldString.length(); index = str.indexOf(oldString, lastIndex); } if (lastIndex < str.length()) { sb.append(str.substring(lastIndex)); } return sb.toString(); } } ``` 这个工具类提供了一些常用的字符串操作方法,例如判断字符串是否为空、判断字符串是否为空白、截取字符串字符串替换等。使用这个工具类可以更加方便地操作字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值