首页 > 编程语言 > 详细

java常用类:String类/可变字符串

时间:2020-12-27 02:07:38      阅读:28      评论:0      收藏:0      [点我收藏+]

String类

  • 字符串时常量,创建后不可改变

    常量存储在字符串池中,当新值赋给字符串时,没有修改数据,而是重新开辟一个空间,之前的值就变成了垃圾

  • 字符串字面值存储在字符串池中,可以共享

    每当创建字符串常量时,会首先检查字符串池,如果该字符串已经存在,那么就直接返回字符串池中的实例引用。

  • 字符串赋值方式:

    1. String s = "Hello";产生一个对象,字符串池中存储

    2. String s = new String("Hello"); 产生两个对象,堆、池各一个

常用方法

  • public int length():返回字符串长度

  • public char charAt(int index):根据下标获取字符

  • public boolean contains(String str):判断当前字符串中是否包含str

  • public char[] toCharArray():将字符串转换成数组

  • public int indexOf(String str):查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1

    int indexOf(String str, int fromIndex) 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

  • public int lastIndexOf(String str):查找str在当前字符串最后一次出现的下标索引

  • public String trim():去掉字符串前后的空格

  • public String toUpperCase():将小写转成大写;

    public String toLowerCase():将大写转成小写

  • public boolean endWith(String str):判断字符串是否以str结尾

    public boolean startsWith(String str):判断字符串是否以str前缀开始

  • public String replace(char oldChar, char newChar):将旧字符串替换成新字符串

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

  • boolean equals(Object anObject) 将此字符串与指定的对象比较。

    boolean equalsIgnoreCase(String anotherString)将此 String 与另一个 String 比较,不考虑大小写。

  • int compareTo(String anotherString) :按字典顺序比较两个字符串。

    • 它是先比较对应字符的大小(ASCII码顺序),如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值,如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方结束。
    • 字符相同时比较长度 返回差值

案例演示

  • 需求:

    已知String str = "this is a text";

    1. 将str中的单词单独获取
    2. 将str中的text替换成practice
    3. 在text前面插入一个easy
    4. 将每个单词的首字母改为大写
public class Demo01 {
    public static void main(String[] args){
        String str="this is a text";
        //1
        System.out.println("==========将str中的单词单独获取==========");
        String[] arr=str.split(" ");
        for (String s:arr)
            System.out.println(s);
        //2
        System.out.println("==========将str中的text替换成practice==========");
        String str2=str.replace("text","practice");
        System.out.println(str2);
        //3
        System.out.println("==========在text前面插入一个easy==========");
        String str3=str.replace("text","easy text");
        System.out.println(str3);
        //4
        System.out.println("==========将每个单词的首字母改为大写==========");
        for (int i=0;i<arr.length;i++){
            //把第一个字符转成大写
            char upperfirst = Character.toUpperCase(arr[i].charAt(0));
            String news = upperfirst + arr[i].substring(1);
            System.out.println(news);
        }
    }
}

this
is
a
text
==========将str中的text替换成practice==========
this is a practice
==========在text前面插入一个easy==========
this is a easy text
==========将每个单词的首字母改为大写==========
This
Is
A
Text


可变字符串

  • StringBuffer: 可变长字符串,jdk1.0提供,运行效率慢,线程安全

  • StringBuilder:可变长字符串,jdk5.0提供,运行效率快,线程不安全

  • 比String效率高

    比String节省内存

public class Demo01 {
    public static void main(String[] args){
        //StringBuffer sb=new StringBuffer();
        StringBuilder sb=new StringBuilder();
        //1.append();追加
        sb.append("java");
        System.out.println(sb.toString());
        sb.append("是最好的语言");
        System.out.println(sb.toString());
        //2.insert();添加
        sb.insert(0,"我在最前面,");
        System.out.println(sb.toString());
        //3,replace();替换
        sb.replace(0,5,"hello");
        System.out.println(sb.toString());
        //4.delete();删除
        sb.delete(0,6);
        System.out.println(sb.toString());
    }
}
//结果:
//java
//java是最好的语言
//我在最前面,java是最好的语言
//hello,java是最好的语言
//java是最好的语言

java常用类:String类/可变字符串

原文:https://www.cnblogs.com/wu-myblog/p/14194043.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!