JAVA常用类(二)

JAVA常用类(二)

三. String类

1. 字符串存储讲解

  • 字符串是常量,创建之后不可改变。
  • 字符串字面值存储在字符串池中,可以共享。
  • String s = “Hello” ;产生一个对象,字符串池中存储。
  • String s = new String(“Hello”); 产生两个对象,堆、池各存储一个。

实例:

public class Application {
    public static void main(String[] args) {
        String name="hello";
        name="world";
        String name2="world";
        String name3 = new String("java");
        String name4 = new String("java");
        System.out.println(name3==name4);
        System.out.println(name3.equals(name4));//直接比较数据
    }
}
----------------结果------------------
false
true

String name=“hello”;执行后

实例1

name=“world”;执行后

实例2

给字符串赋值时,并没有修改数据,而是重新开辟空间。这就是字符串的不可变性

String name2=“world”;执行后

实例3新建字符串变量时,会优先从字符串池中查找,有相同的则直接引用。这就实现了字符串的共享

String name3 = new String(“java”);执行后

产生两个对象,堆、池各存储一个。在运行时堆中的对象没有值,只是引用字符串池中的值。

实例4

String name4 = new String(“java”);
System.out.println(name3==name4);执行后:

在这里插入图片描述因为指向的对象不一样,所以结果为false。

System.out.println(name3.equals(name4));执行后

直接用字符串池中的数据相比较,相同所以结果为true。

2. 常用方法

length()方法

public int length()

  • 返回字符串的长度。
public class Application {
    public static void main(String[] args) {
        String str="Hello World!";
        //length()方法
        System.out.println(str.length());
    }
}
-------------------结果-----------------------
12
charAt()方法

public char charAt(int index)

  • 根据下标获取字符。
public class Application {
    public static void main(String[] args) {
        String str="Hello World!";
        //charAt()方法
        System.out.println(str.charAt(0));
        System.out.println(str.charAt(str.length()-1));
    }
}
-----------------结果----------------------
H
!
contains()方法

public boolean contains (String str)

  • 判断当前字符串中是否包含str。
public class Application {
    public static void main(String[] args) {
        String str="Hello World!";
        //contains()方法
        System.out.println(str.contains("Hello"));
        System.out.println(str.contains("world"));
    }
}
-----------------结果----------------------
true
false
toCharArray()方法

public char[] toCharArray ()

  • 将字符串转换成数组。
import java.util.Arrays;

public class Application {
    public static void main(String[] args) {
        String str="Hello World!";
        //toCharArray ()
        char[] str1=str.toCharArray();
        for (char c : str1) {System.out.print(c+"  ");}
		System.out.println("\n"+Arrays.toString(str.toCharArray()));
    }
}
-----------------结果----------------------
H  e  l  l  o     W  o  r  l  d  !  
[H, e, l, l, o,  , W, o, r, l, d, !]
indexOf()方法

public int indexOf(String str)

查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1。

public class Application {
    public static void main(String[] args) {
        String str="Hello World!";
        //indexOf()
        System.out.println("字符l首次出现的位置"+str.indexOf("l"));
        System.out.println("字符l从下标3开始出现的位置"+str.indexOf("l",3));
    }
}
-----------------结果----------------------
字符l首次出现的位置2
字符l从下标3开始出现的位置3
lastIndexOf()方法

public int lastIndexOf(String str)

查找字符串在当前字符串中最后一次出现的下标索引。

public class Application {
    public static void main(String[] args) {
        String str="Hello World!";
        //lastIndexOf()
        System.out.println("字符l最后出现的位置"+str.lastIndexOf("l"));
        System.out.println("字符o最后出现的位置"+str.lastIndexOf("o"));
        System.out.println("字符l到第8个之前最后出现的位置"+str.lastIndexOf("l",8));
    }
}
-----------------结果----------------------
字符l最后出现的位置9
字符o最后出现的位置7
字符l到第8个之前最后出现的位置3
trim()方法

public String trim()

去掉字符串前后的空格。

public class Application {
    public static void main(String[] args) {
        String str1="  Hello World!  ";
        //trim()
        System.out.println(str.trim());
    }
}
-----------------结果----------------------
Hello World!
toUpperCase()/toLowerCase()方法

public String toUpperCase()
public String toLowerCase()

将小写转成大写/将大写转成小写。

public class Application {
    public static void main(String[] args) {
        String str="Hello World!";
        //toUpperCase()/toLowerCase()
        System.out.println(str.toUpperCase());
        System.out.println(str.toLowerCase());
    }
}
-----------------结果----------------------
HELLO WORLD!
hello world!
endsWith()/startsWith()方法

public boolean endsWith (String str)
public boolean startsWith() (String str)

判断字符串是否以str结尾/判断字符串是否以str开头。

public class Application {
    public static void main(String[] args) {
        String str="Hello World!";
        //endsWith()/startsWith()
        System.out.println(str.endsWith("!"));
        System.out.println(str.startsWith("Hel"));
    }
}
-----------------结果----------------------
true
true
replace()方法

public String replace (char oldChar, char newChar)

将旧字符串替换成新字符串

public class Application {
    public static void main(String[] args) {
        String str="Hello World!";
        //replace()
        System.out.println(str.replace("l","L"));
        System.out.println(str.replace("He","ll"));
    }
}
-----------------结果----------------------
HeLLo WorLd!
llllo World!
split()方法

public String[] split(String str)

根据str做拆分。

public class Application {
    public static void main(String[] args) {
        String str="Hel-lo Wor-ld!";
        String str2="H--e  l-l o W-o  r-l d--!";
        //split()
        System.out.println(Arrays.toString(str.split(" ")));
        System.out.println(Arrays.toString(str.split("[ -]")));//同时用空格或-分割
        System.out.println(Arrays.toString(str2.split("[ -]")));
        System.out.println(Arrays.toString(str2.split("[ -]+")));//表示可以多个空格或-分隔
    }
}
-----------------结果----------------------
[Hel-lo, Wor-ld!]
[Hel, lo, Wor, ld!]
[H, , e, , l, l, o, W, o, , r, l, d, , !]
[H, e, l, l, o, W, o, r, l, d, !]
equals()方法

public boolean equals(Object anObject)

比较两个字符串是否相同

public class Application {
    public static void main(String[] args) {
        String str1="aaa";
        String str2="AAA";
        System.out.println(str1.equals(str2));
        System.out.println(str1.equalsIgnoreCase(str2));
    }
}
-----------------结果----------------------
false
true
compareTo()方法

public int compareTo(String anotherString)

比较两个字符串字符大小,输出差值

public class Application {
    public static void main(String[] args) {
        String str1="aaa";//a=97
        String str2="AAA";//A=65
        String str3="aaaaaa";
        System.out.println(str1.compareTo(str2));//减去差值
        System.out.println(str1.compareTo(str3));//长度相减
    }
}
32
-3
案例演示

需求:
已知:String str = “this is a text”;

  • 1.将str中的单词单独获取出来
  • 2.将str中的text替换为practice
  • 3.在text前面插入一个easy
  • 4.将每个单词的首字母改为大写
public class Application {
    public static void main(String[] args) {
        String str="this is a text";
        //1.将str中的单词单独获取出来
        System.out.println("将str中的单词单独获取出来");
        String[] str1=str.split(" ");
        for (String s : str1) {
            System.out.println(s);
        }
        //2.将str中的text替换为practice
        System.out.println("将str中的text替换为practice");
        String str2 = str.replace("text","practice");
        System.out.println(str2);
        //3.在text前面插入一个easy
        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<str1.length;i++){
            if(str1[i].length()>1){
                str1[i]=str1[i].toUpperCase().charAt(0)+str1[i].substring(1,str1[i].length());
            }else{
                str1[i]=str1[i].toUpperCase();
            }
            String str4 = (i==str1.length-1)?"":" ";
            System.out.print(str1[i]+str4);
        }
    }
}
---------------------结果-------------------------
将str中的单词单独获取出来
this
is
a
text
将str中的text替换为practice
this is a practice
在text前面插入一个easy
this is a easy text
将每个单词的首字母改为大写
This Is A Text

3. 可变字符串

  • StringBuffer:可变长字符串,JDK1.0提供,运行效率慢、线程安全。
  • StringBuilder:可变长字符串,JDK5.0提供,运行效率快、线程不安全。

区别:

  • 效率要比String高
  • 比String节省内存
append()方法 添加

public synchronized StringBuffer append(String str)

将字符串str添加到目标尾部

public class Application {
    public static void main(String[] args) {
        StringBuffer stb= new StringBuffer();
        //append()方法 添加
        stb.append("abc");
        System.out.println(stb);
    }
}
-------------------结果------------------
abc
insert()方法 插入

public synchronized StringBuffer insert(int offset, String str)

将字符串str从下标offset前插入目标

public class Application {
    public static void main(String[] args) {
        StringBuffer stb= new StringBuffer();
        stb.append("abc");
        //insert()方法  插入
        stb.insert(2,"abc");
        System.out.println(stb);
    }
}
-------------------结果------------------
ababcc
replac()方法 修改

public synchronized StringBuffer replace(int start, int end, String str)

将从下标start开始至下标end前的字符替换为str

public class Application {
    public static void main(String[] args) {
        StringBuffer stb= new StringBuffer();
        stb.append("abc");
        //replace()方法  修改
        stb.replace(0,stb.length(),"aabbccdd");
        System.out.println(stb);
    }
}
-------------------结果------------------
aabbccdd
delete()方法 删除

public synchronized StringBuffer delete(int start, int end)

将从下标start开始至下标end前结束的字符删除

public class Application {
    public static void main(String[] args) {
        StringBuffer stb= new StringBuffer();
        stb.append("abc");
        //delete()方法  删除
        stb.delete(0,2);
        System.out.println(stb);
    }
}
-------------------结果------------------
c
reverse()方法 翻转

public synchronized StringBuffer reverse()

将字符串翻转,第一个变最后一个,依次改变

public class Application {
    public static void main(String[] args) {
        StringBuffer stb= new StringBuffer();
		stb.append("ccdd");
        //reverse()方法  翻转
        stb.reverse();
        System.out.println(stb);
    }
}
-------------------结果------------------
ddcc
效率验证

验证String类型和StringBuilder的执行速度

public class Application {
    public static void main(String[] args) {
        //String时间测量
        long start=System.currentTimeMillis();
        String str="";
        for (int i = 0; i < 9999; i++) {
            str+=i;
        }
        long end = System.currentTimeMillis();
        System.out.println("String用时"+(end-start));
        //StringBuilder时间测量
        long start1=System.currentTimeMillis();
        StringBuilder stb=new StringBuilder();
        for (int i = 0; i < 9999; i++) {
            stb.append(i);
        }
        long end1 = System.currentTimeMillis();
        System.out.println("StringBuilder用时"+(end1-start1));
    }
}
-----------------结果--------------------
String用时555
StringBuilder用时1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值