Java中String字符串常用方法

方法及测试一:
 
int length() 返回字符串的长度: return value.length
 
char charAt(int index) 返回某索引处的字符 return value[index]
 
boolean isEmpty() 判断是否是空字符串: return value.length == 0
 
String toLowerCase() 使用默认语言环境,将 String 中的所有字符转换为小写
 
String toUpperCase() 使用默认语言环境,将 String 中的所有字符转换为大写
 
String trim() 返回字符串的副本,忽略前导空白和尾部空白
 
boolean equals(Object obj) 比较字符串的内容是否相同
 
boolean equalsIgnoreCase(String anotherString) equals 方法类似,忽略大
小写
 
String concat(String str) 将指定字符串连接到此字符串的结尾。 等价于用“ +
 
int compareTo(String anotherString) 比较两个字符串的大小
 
String substring(int beginIndex) 返回一个新的字符串,它是此字符串的从
beginIndex 开始截取到最后的一个子字符串。
 
String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字
符串从 beginIndex 开始截取到 endIndex( 不包含 ) 的一个子字符串。
  @Test
    public void Test1(){
        String s1 = " H e l l O ";
        String s2 = "hello";
        String s3 = " HELLO ";
        System.out.println(s1.length());//11
        System.out.println(s1.charAt(3));//e
        System.out.println(s1.isEmpty());//false
        
        System.out.println(s1.toLowerCase());//" h e l l o "
        System.out.println(s1.toUpperCase());//" H E L L O "
        
        System.out.println(s1.trim());//"H e l l O"
        
        System.out.println(s1.equals(s2));//false
        System.out.println(s2.equalsIgnoreCase(s3.trim()));//true
        
        System.out.println(s2.compareTo(s3.trim().toLowerCase()));//0 (此返回值为s2的大小减去s1的大小)

        System.out.println(s2.substring(3));//"lo"
        System.out.println(s2.substring(1,4));//"ell" 左闭右开
    }

 方法及测试二:

boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束
 
boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始
 
boolean startsWith(String prefix, int toffset) 测试此字符串从指定索引开始的子字符串是否以指定前缀开始
 
boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true
 
int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引
 
int indexOf(String str, int fromIndex) 返回指定子字符串在此字符串中第一次出
现处的索引,从指定的索引开始
 
int lastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引
 
int lastIndexOf(String str, int fromIndex) 返回指定子字符串在此字符串中最后
一次出现处的索引,从指定的索引开始反向搜索 (注:indexOf lastIndexOf 方法如果未找到都是返回 -1)
 
replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
 
replace(CharSequence target, CharSequence replacement) 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
 
@Test
    public void Test2(){
        String str1 = "hello";

        System.out.println(str1.endsWith("lo"));//true
        System.out.println(str1.startsWith("he"));//true
        System.out.println(str1.startsWith("l", 3));//true

        System.out.println(str1.contains("el"));//true

        str1 = "hellohellohello world";
        System.out.println(str1.indexOf("hello"));//0
        System.out.println(str1.indexOf("hello", 2));//5

        System.out.println(str1.lastIndexOf("hello"));//10
        System.out.println(str1.lastIndexOf("hello", 7));//5

        System.out.println(str1.replace("world", "java"));//"hellohellohello java"
        System.out.println(str1);//"hellohellohello world"
        System.out.println(str1.replace('h','!'));//"!ello!ello!ello world"


    }
 
 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值