String类

特点
- 字符串不变:字符串的值在创建后不能被更改。[[值传递和引用传递#String类型|String类引用传递]]
| package com.zhou.day0822; |
| |
| |
| public class Demo01 { |
| public static void main(String[] args) { |
| |
| |
| int a = 10; |
| |
| |
| |
| |
| |
| String s = "狗子"; |
| |
| System.out.println(s); |
| s = s + "bb"; |
| System.out.println(s); |
| } |
| } |

- 上图一个创建了三个对象,但是s的地址从始至终都没有变。此即String类的值不可变性!
- 因为String对象是不可变的,所以它们可以被共享。
| |
| String s = "狗子"; |
| |
| System.out.println(s); |
| |
| System.out.println(s); |
| |
| |
| String s1 = "狗子"; |
| System.out.println(s == s1); |
3. "abc" 等效于 char[] data={ 'a' , 'b' , 'c' } 。
| |
| char[] c = {'a','b','c'}; |
| String str = new String(c); |
| System.out.println(str); |
使用步骤

| package com.zhou.day0822; |
| |
| public class Demo03 { |
| public static void main(String[] args) { |
| |
| String str = new String(); |
| |
| |
| char[] c = {'a','b','c'}; |
| String str2 = new String(c); |
| |
| |
| byte[] bytes = {97,98,99}; |
| String str3 = new String(bytes); |
| } |
| } |
常用方法
- 判断功能的方法:
public boolean equals(Object anObject)
:将此字符串与指定对象进行比较。
| public class Demo04 { |
| public static void main(String[] args) { |
| String s1 = "Hello"; |
| String s2 = "Hello"; |
| String s3 = "HEllo"; |
| String s4 = new String("Hello"); |
| System.out.println(s1 == s2); |
| |
| System.out.println(s1.equals(s2)); |
| String s5 = "He"; |
| s5 = s5+"llo"; |
| System.out.println(s5.equals(s1)); |
| System.out.println(s5 == s1); |
| } |
| } |
public boolean equalsIgnoreCase(String anotherString)
:将此字符串与指定对象进行比较,忽略大小写。
| public class Demo04 { |
| public static void main(String[] args) { |
| String s1 = "Hello"; |
| String s2 = "Hello"; |
| String s3 = "HEllo"; |
| System.out.println(s2.equalsIgnoreCase(s3)); |
- 获取功能的方法
public int length();
:返回此字符串的长度。
public String concat(String str);
:将指定的字符串连接到该字符串的末尾。
public char charAt(int index);
:返回指定索引处的char值。
public int indexOf(String str);
:返回指定子字符串第一次出现在该字符串内的索引。
public String substring(int beginIndex);
:返回一个子字符串,从beginindex开始截取字符串到字符串结尾。
public String substring(int brginIndex,int endIndex);
:返回一个子字符串,从beginindex到endindex截取字符串。含beginindex,不含endindex。(左闭右开)
| package com.zhou.day0822; |
| |
| public class Demo05 { |
| public static void main(String[] args) { |
| |
| String s = "helloworldhello"; |
| System.out.println(s.length()); |
| |
| |
| s = s.concat("666"); |
| System.out.println(s); |
| |
| |
| char c = s.charAt(5); |
| System.out.println(c); |
| |
| |
| int i = s.indexOf("hello"); |
| System.out.println(i); |
| |
| |
| String str = "yedemingmingshu"; |
| System.out.println(str.substring(4)); |
| |
| |
| String str1 = "jishizaixiaodefanyenengyuanhang"; |
| System.out.println(str1.substring(2,7)); |
| } |
| } |
- 转换功能的方法
public char[] toCharArray();
:将此字符串转换为新的字符数组。
public byte[] getBytes();
:适用平台默认字符集将该String编码转换为新的字节数组。
public String replace(CharSequence target,CharSequence replacement);
:将于target匹配的字符串使用replacement字符串替换。
| public class Demo06 { |
| public static void main(String[] args) { |
| |
| String str = "abcabc"; |
| char[] c = str.toCharArray(); |
| for (int i = 0;i<c.length;i++) { |
| System.out.println(c[i]); |
| } |
| |
| |
| byte[] b = str.getBytes(); |
| for (int i = 0;i<b.length;i++) { |
| System.out.println(b[i]); |
| } |
| |
| |
| String s = str.replace("bc","aa"); |
| System.out.println(s); |
| } |
| } |
- 分割功能的方法
public String[] split(String regex);
:将此字符串按照给定的regex(规则)拆分为字符串数组。
| package com.zhou.day0822; |
| |
| public class Demo07 { |
| |
| public static void main(String[] args) { |
| String s = "aa-bb-cc-dd"; |
| String[] str = s.split("-"); |
| for (int i = 0;i<str.length;i++) { |
| System.out.println(str[i]); |
| } |
| } |
| } |
本文作者:小周和java
本文链接:https://www.cnblogs.com/xzandjava/p/16612342.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步