java string类常用方法

  1. length():返回字符串的长度。
    String str = "hello world";
    int len = str.length(); // len = 11

  2. charAt(index):返回指定索引位置的字符。
    char ch = str.charAt(1); // ch = 'e'
  3. substring(beginIndex, endIndex):返回一个子字符串,从beginIndex开始,到endIndex结束(不包括endIndex)。
    String subStr = str.substring(0, 5); // subStr = "hello"

  4. equals(obj):比较字符串是否相等,返回布尔值。
    String str1 = "hello";
    String str2 = "world";
    boolean result1 = str1.equals(str2); // result1 = false
    
    String str3 = "hello";
    String str4 = "hello";
    boolean result2 = str3.equals(str4); // result2 = true
  5. equalsIgnoreCase(str):比较字符串是否相等,忽略大小写,返回布尔值。
    String str1 = "hello";
    String str2 = "HELLO";
    boolean result1 = str1.equalsIgnoreCase(str2); // result1 = true
    
    String str3 = "world";
    String str4 = "Hello";
    boolean result2 = str3.equalsIgnoreCase(str4); // result2 = false
    
  6. indexOf(str):返回字符串中第一次出现指定子字符串的索引位置,如果未找到返回-1。
    String str = "hello world";
    int index1 = str.indexOf("o"); // index1 = 4
    int index2 = str.indexOf("llo"); // index2 = 2
    int index3 = str.indexOf("x"); // index3 = -1
  7. replace(oldChar, newChar):返回一个新的字符串,将所有旧字符替换为新字符。
    String str = "hello world";
    String newStr = str.replace("o", "0"); // newStr = "hell0 w0rld"
    
  8. split(regex):将字符串拆分为一个字符串数组,使用正则表达式分隔。
    String str = "hello,world,java";
    String[] arr = str.split(","); // arr = {"hello", "world", "java"}
    

  9. toUpperCase():将字符串转换为大写。
    String str = "hello world";
    String newStr = str.toUpperCase(); // newStr = "HELLO WORLD"
    

  10. toLowerCase():将字符串转换为小写。
    String str = "HELLO WORLD";
    String newStr = str.toLowerCase(); // newStr = "hello world"
    
  11. startsWith(prefix):判断字符串是否以指定的前缀开始,返回布尔值。
    String str = "hello world";
    boolean result = str.startsWith("hello"); // result = true
  12. endsWith(suffix):判断字符串是否以指定的后缀结束,返回布尔值
    String str = "hello world";
    boolean result = str.endsWith("world"); // result = true
  13. contains(str):判断字符串是否包含指定的子字符串,返回布尔值。
    String str = "hello world";
    boolean result = str.contains("lo"); // result = true
    
  14. isEmpty():判断字符串是否为空字符串,返回布尔值。
    String str1 = "";
    String str2 = "hello";
    boolean result1 = str1.isEmpty(); // result1 = true
    boolean result2 = str2.isEmpty(); // result2 = false
    
  15. trim():返回一个新的字符串,去除字符串前后的空格
    String str = "   hello world   ";
    String newStr = str.trim(); // newStr = "hello world"
  16. compareTo(str):比较字符串与指定字符串的字典顺序,返回一个整数,如果字符串在字典中排在指定字符串之前,则返回一个负数,如果它在指定字符串之后,则返回一个正数,如果它与指定字符串相等,则返回0。
    String str1 = "hello";
    String str2 = "world";
    int result1 = str1.compareTo(str2); // result1 < 0
    int result2 = str2.compareTo(str1); // result2 > 0
    int result3 = str1.compareTo("hello"); // result3 = 0
    
  17. valueOf(obj):将指定的对象转换为字符串表示形式,返回一个新的字符串。
    int num = 123;
    String str = String.valueOf(num); // str = "123"
  18. format(format, args):返回一个格式化字符串,使用指定的格式字符串和参数。
    String str = String.format("Hello %s, your age is %d", "John", 30); // str = "Hello John, your age is 30"
    
  19. matches(regex):判断字符串是否匹配指定的正则表达式,返回布尔值。
    String str1 = "hello";
    String str2 = "hello123";
    boolean result1 = str1.matches("[a-z]+"); // result1 = true
    boolean result2 = str2.matches("[a-z]+"); // result2 = false
    
  20. join(delimiter, elements):将多个字符串连接成一个字符串,使用指定的分隔符分隔,返回一个新的字符串。
    String str = String.join(",", "hello", "world", "java"); // str = "hello,world,java"
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值