java常用类-String

3.String类
字符串是常量,创建之后不可改变;
字符串字面值存储在字符串池(方法区)中;

查询字符串

  • public int length() : 返回字符串长度;
  • public char charAt(int index) : 根据下标获取字符;
  • public boolean contains(String str) : 判断当前字符串中是否包含str;
  • public boolean startWith(String str) : 判断字符串是否以str开头;
  • public boolean endWith(String str) : 判断字符串是否以str结尾;
  • public int indexOf(String str) : 查找str首次出现的下标,存在则返回该下标,不存在则返回-1;
  • public int lastIndexOf(String str) : 查找str最后出现的下标,存在则返回该下标,不存在则返回-1;
    修改字符串
  • public String toUpperCase() : 将小写转换成大写;
  • public String toLowerCase() : 将大写转换成小写;
  • public String trim() : 去掉字符串首尾的空格;
  • public String replace(char oldChar,char newChar) : 将就字符(串)替换成新字符(串);
  • public char[] toCharArray() : 将字符串转换成字符数组;
  • public String[] split() : 分割字符串为字符串数组;
  • public String subString(int beginIndex,int endIndex) : 截取部分字符串;
    比较字符串
  • public boolean equals(String str) : 比较两个字符串内容是否相等;
  • public boolean equalsIngoreCase(String str) : 忽略大小写比较两个字符串内容是否相等;
  • public int compareTo(String str) : 比较两个字符串大小(在字符编码中的顺序),
    从第一个字母开始,只比较一个字母,
    如果长度不同且包含另一个字符串,则比较字符串长度;
  • public boolean matches(String radix) : 判断是否属于正则表达式
public class StringDemo {
    public static void main(String[] args) {
        System.out.println("---0.创建字符串-------------------");
        //创建字符串
        String string="hello";//hello 常量存储在字符串池中
        string="world";//并不修改数据,而是在常量池中新增world,并重新指向
        String string1="world";
        //字符串另一种创建方式
        String str=new String("java");//在堆中创建地址,指向常量池中创建的java
        String str1=new String("java");
        System.out.println(str==str1);//false
        System.out.println(str.equals(str1));//true

        System.out.println("---1.查询字符串-------------------");
        String s = "Hello, the World! ";
        System.out.println("字符串长度:"+s.length());
        System.out.println("索引3的字符:"+s.charAt(3));
        System.out.println("是否包含is:"+s.contains("the"));
        System.out.println("是否以Ja开头:"+s.startsWith("the"));
        System.out.println("是否以x结束:"+s.endsWith("!"));
        System.out.println("第一次出现a的索引:"+s.indexOf("a"));
        System.out.println("最后一次出现a的索引:"+s.lastIndexOf("a"));

        System.out.println("---2.修改字符串-------------------");
        System.out.println("转换为大写:"+s.toUpperCase());
        System.out.println("转换为小写:"+s.toLowerCase());
        System.out.println("替换字符:"+s.replace('o','x'));
        System.out.println("替换字符串:"+s.replace("World","Java"));
        System.out.println("去掉头尾的空格:"+s.trim());
        System.out.println("转化为字符数组:"+s.toCharArray().toString());
        System.out.println("以子字符串拆分字符串:"+s.split(" "));
        System.out.println("截取字符串:"+s.substring(0,5));

        System.out.println("---3.比较字符串-------------------");
        String s1="Hello world";
        String s2="Hello world!";
        String s3="Hello WORLD";
        System.out.println("比较两个字符串是否相等: "+s1.equals(s2));//false
        System.out.println("(Hello world) equals (Hello WORLD) ? "+s1.equals(s3));//false
        System.out.println("忽略大小写比较是否相等: "+s1.equalsIgnoreCase(s3));//true
        String s4="abc";
        String s5="xyz";
        System.out.println("比较字符串大小"+s4.compareTo(s5));
        String s6="xyzab";
        System.out.println("比较字符串大小"+s5.compareTo(s6));
    }
}

4.StringBuffer/StringBuilder类
StringBuffer: 可变长字符串,JDK1.0提供,运行效率慢、线程安全;
StringBuilder: 可变长字符串,JDK1.5提供,运行效率快,线程不安全;
二者功能完全相同

public StringBuffer append(String str) : 追加字符串;
public StringBuffer insert(int index,String str) : 插入字符串;
public StringBuffer replace(int beginIndex,int endIndex,String str) : 替换字符串;
public StringBuffer delete(int beginIndex,int endIndex) : 删除字符串,,含头不含尾;

public class StringBuf_Bui {
    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer("java");
        System.out.println(sb.toString());//java
        sb.append(" is excellent");
        System.out.println(sb.toString());//java is excellent
        sb.insert(0,"The ");
        System.out.println(sb.toString());//The java is excellent
        sb.replace(4,8,"C++");
        System.out.println(sb.toString());//The C++ is excellent
        sb.delete(0,4);
        System.out.println(sb.toString());//C++ is excellent
    }
}
posted @   老李学Java  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示