<十>java常用类(2)

Java常用类—String类(二)

String的常用方法1

一.方法概述

  1. int length():返回字符串的长度
  2. char charAt(int index):返回某索引处的字符
  3. boolean isEmpty():判断是否是空字符串
  4. String toLowerCase():使用默认语言环境,将String中的所有字符转换为小写
  5. String toUpperCase():使用默认语言环境,将String中的所有字符转换为大写
  6. String trim():返回字符串的副本,忽略前导空白和尾部空白
  7. boolean equals(Object obj):比较字符串的内容是否相同
  8. boolean equalslgnoreCase(String anotherString):与equals方法类似,忽略大小写
  9. String concat(String str):将指定字符串连接到此字符串的结尾,等价于“+”
  10. int compareTo(String anotherString):比较两个字符串的大小
  11. String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串
  12. String substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串从beginIndex开始到endIndex(包前不包后)的一个字符串

二.方法测试

package StringClass;

import org.junit.Test;

import java.util.Locale;

public class StringMethodTest {
    @Test
    public void test1(){
        String s1="helloworld";
        System.out.println(s1.length());//输出:10;
        System.out.println("----------------------------");
        System.out.println(s1.charAt(0));//输出h
        System.out.println(s1.charAt(9));//输出d
        //System.out.println(s1.charAt(10));//超出数组长度
        System.out.println("----------------------------");
        //isEmpty():判断是否为空字符串
        System.out.println(s1.isEmpty());// false
        System.out.println("----------------------------");
        String s2=s1.toUpperCase();
        System.out.println(s1);//helloworld,不可变性,s1是没变的
        System.out.println(s2);//HELLOWORLD
        System.out.println("----------------------------");
        String s3=" he  llo  world";
       String s4= s3.trim();
        System.out.println(s3);//没变
        System.out.println(s4);//去除了首位空格,中间的还是有
        System.out.println("----------------------------");
        String s5="XuQ";
        String s6="xuq";
        System.out.println(s5.equalsIgnoreCase(s6));//true;
        System.out.println("----------------------------");
        System.out.println(s5.concat("abc"));//XuQabc
        System.out.println("----------------------------");
        System.out.println(s5.compareTo(s6));//-32,这个数小于后面的数
        System.out.println(s5.compareTo(s3));//56
        System.out.println("----------------------------");
        String s7="江西农业大学";
        String s8 = s7.substring(2);
        System.out.println(s7);//不变
        System.out.println(s8);//农业大学
        String s9 = s7.substring(2, 4);//农业
        System.out.println(s9);
        //包前不包后,从01开始,左闭右开
        



    }
}

String的常用方法2

一.方法概述

  1. boolean endsWith(String suffix ):测试此字符串是否以指定的后缀结束
  2. boolean startsWith(String prefix ):测试此字符串是否以指定的前缀结束
  3. boolean startsWith(String prefix,int toffset ):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
  4. boolean contains(CharSequence s):当且仅当此字符串包含指定的char值序列时,返回true
  5. int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
  6. int indexOf(String str,int fromIndex):返回指定字符串在此字符串中第一次出现处的索引,从指定的索引开始
  7. int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引
  8. int lastIndexOf(String str,int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索

注:indexOf和lastIndexOf方法如果未找到都是返回-1

二.代码测试

package StringClass;

import org.junit.Test;

public class StringMethodTest2 {
    @Test
    public void test1(){
        String s1="helloworld";
        boolean b1 = s1.endsWith("ld");//判断字符串是否以指定字符后缀
        System.out.println(b1);//true
        boolean b2 = s1.endsWith("h");
        System.out.println(b2);//false
        boolean b3 = s1.startsWith("He");
        System.out.println(b3);//false
        boolean b4= s1.startsWith("ll", 2);
        System.out.println(b4);//true,包括开始的
        System.out.println("----------------------");
        boolean b5 = s1.contains("llow");
        System.out.println(b5);//true,测试是否包含指定字符块
        System.out.println("----------------------");
        String s2="abcdabdeffg";
        System.out.println(s2.indexOf("b"));//1  测试指定字符串第一次出现的索引
        System.out.println(s2.indexOf("h"));//-1,当没找到都是返回-1
        System.out.println(s2.indexOf("d", 3));//从指定索引位置开始找
        System.out.println("----------------------");
        System.out.println(s2.lastIndexOf("b"));//从后往前找
        System.out.println(s2.lastIndexOf("d", 6));//返回的索引还是从前往后的索引!!
        


    }
}

String的常用方法3

一.方法概述

1)替换:

  1. String replace(char oldChar,char newChar):返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的

  2. String replace(CharSequence target,CharSequence replacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串(换多个字符串)

  3. String replaceAll(String regex ,String replacement):使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串

  4. String replaceFirst(String regex ,String replacement):使用给定的replacement替换此字符串所有匹配给定的正则表达式的第一个子字符串

    2)匹配

  5. boolean matches(String regex):告知此字符串是否匹配给定的正则表达式

    3)切片

  6. String[]split(String regex):根据给定正则表达式的匹配拆分此字符串

  7. String[]split(String regex,int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中

二.方法测试

package StringClass;

import org.junit.Test;

public class StringMethodTest3 {
    @Test
    public void test1(){
        String s1="JiangXiNongYeDaXue";
        String s2 = s1.replace("NongYe", "ChaiJing");
        System.out.println(s1);//不变
        System.out.println(s2);//JiangXiChaiJingDaXue
        String s3="北京大学";
        String s4 = s3.replace("北京", "清华");
        System.out.println(s4);
 

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zero摄氏度

感谢鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值