5

Java String类 - 愚生浅末

 1 year ago
source link: https://www.cnblogs.com/kohler21/p/16436767.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

 字符串广泛应用 在 Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。
jdk中提供非常多的字符和字符串操作方法及构造方法,这里只介绍一些常用的方法和构造方法。完整的String类下的方法可以参考官方的API文档。
本地API文档下载: https://kohler.lanzouv.com/ikIfV078pbhe
在线API文档:https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html

API文档截图:

62bfa2991d64b07066289ce3.jpg

直接使用字面值

可以直接定义String类型的变量直接给其赋值一个字符串字面值
例:



String name = "愷龍";

使用构造方法

可以使用String中定义的构造方法来创建对象。String类下有非常多的构造方法,这里只介绍几个常用的。

String()

public String();
初始化新创建的字符串对象,使其表示空字符序列。

示例代码:



public static void main(String[] args) { //使用无参构造创建。字符串的内容为空 相当于 "" String s1 = new String(); }

String(byte[] bytes)

String(byte[] bytes);
将数组转换为字符串。
示例代码:



public static void main(String[] args) { byte[] bytes = {68,74,84,85}; String s = new String(bytes); System.out.println(s);//输出结果为DJTU,这里是将数字通过ASC码表转换为了字母 }

String(byte[] bytes, int offset, int length)

通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
参数:
bytes:要解码为字符的 byte
offset: 要解码的第一个 byte 的索引
length: 要解码的 byte 数 的长度

示例代码:



public static void main(String[] args) { byte[] bytes = {68,74,84,85}; String s = new String(bytes,0,2); System.out.println(s);//输出结果为DJ,从第0个开始长度为2个 String s2 = new String(bytes,0,1); System.out.println(s2);//输出结果为D,从第0个开始长度为1个 }

结果:
String(char[] value)

转换字符数组为字符串类
示例代码:



public static void main(String[] args) { char[] chars = {'D','J','T','U'}; String s = new String(chars); System.out.println(s);//输出结果为DJTU }

结果:
String(char[] value, int offset, int count)

参数:
value - 作为字符源的数组。
offset - 初始偏移量。
count - 长度。
就是在数组value上选取一部分成为String对象。
示例代码:



public static void main(String[] args) { char[] chars = {'D','J','T','U'}; String s = new String(chars,0,1); System.out.println(s);//输出结果为D String ss = new String(chars,0,2); System.out.println(s2);//输出结果为DJ }
方法 解释
String[] split(String regex) 把一个字符串按照指定的分隔符切割成多个字符串,把多个字符串放在一个字符串数组中返回
char[] toCharArray(); 把一个字符串的内容转换成一个字符数组
byte[] getBytes(); 把一个字符串的内容转换成一个byte数组
String substring(int index); 把某个字符串从index索引开始截取到最后
String substring(int begin,int end) 把某个字符串索引begin到索引end截取出来
boolean equals(Object anObject) 判断两个字符串的内容是否相同

split方法演示



public static void main(String[] args) { String s = "DJTU,China,LiaoNing,DaLian"; String[] strs = s.split(",");//以,分割 for (int i = 0; i < strs.length; i++) { System.out.println(strs[i]); } }

toCharArray方法演示



public static void main(String[] args) { String s = "DJTU"; char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { System.out.println(chars[i]); } }

getBytes方法演示



public static void main(String[] args) { String s = "DJTU"; byte[] bytes = s.getBytes();//按照ASC码表转换为数字 for (int i = 0; i < bytes.length; i++) { System.out.println(bytes[i]); } }

substring方法演示



public static void main(String[] args) { String s = "DJTU"; String substring = s.substring(1);//从第[1]个开始截取 System.out.println(substring); }


public static void main(String[] args) { String s = "DJTU"; String substring = s.substring(1,2);//从第[1]个开始到第[2]个结束(不包含第[2]个) System.out.println(substring); }

equals方法演示



public static void main(String[] args) { String s = "DJTU"; String s2 = "DJTU"; String s3 = "DJTUD"; boolean flag = s.equals(s2); boolean flag1 = s.equals(s3); System.out.println(flag);//输出true System.out.println(flag1);//输出false }
  1. 一个字符串一旦创建其内容是永远不会变的
  2. 字符串效果上相当于是char[]字符数组,但是底层其实是byte[]字节数组

如图片失效等问题请参阅公众号文章:https://mp.weixin.qq.com/s/_vctLlUqXqy7_vWBwYaFgg

欢迎关注我的公众号:"愚生浅末",一起交流学习。

__EOF__


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK