目录
概述
字符串广泛应用 在 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文档截图:
对象创建
直接使用字面值
可以直接定义String类型的变量直接给其赋值一个字符串字面值
例:
<pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">String name = "愷龍";</pre>n
使用构造方法
可以使用String中定义的构造方法来创建对象。String类下有非常多的构造方法,这里只介绍几个常用的。
String()
public String();n
初始化新创建的字符串对象,使其表示空字符序列。
示例代码:
<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n //使用无参构造创建。字符串的内容为空 相当于 ""n String s1 = new String();n }</pre>n
String(byte[] bytes)
String(byte[] bytes);n
将数组转换为字符串。
示例代码:
<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {nn byte[] bytes = {68,74,84,85};n String s = new String(bytes);n System.out.println(s);//输出结果为DJTU,这里是将数字通过ASC码表转换为了字母n }</pre>n
结果:
String(byte[] bytes, int offset, int length)
通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
参数:
bytes:要解码为字符的 byte
offset: 要解码的第一个 byte 的索引
length: 要解码的 byte 数 的长度
示例代码:
<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n byte[] bytes = {68,74,84,85};n String s = new String(bytes,0,2);n System.out.println(s);//输出结果为DJ,从第0个开始长度为2个n String s2 = new String(bytes,0,1);n System.out.println(s2);//输出结果为D,从第0个开始长度为1个n }</pre>n
结果:
String(char[] value)
转换字符数组为字符串类
示例代码:
<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n char[] chars = {'D','J','T','U'};n String s = new String(chars);n System.out.println(s);//输出结果为DJTUn }</pre>n
结果:
String(char[] value, int offset, int count)
参数:
value - 作为字符源的数组。
offset - 初始偏移量。
count - 长度。
就是在数组value上选取一部分成为String对象。
示例代码:
<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n char[] chars = {'D','J','T','U'};n String s = new String(chars,0,1);n System.out.println(s);//输出结果为Dn String ss = new String(chars,0,2);n System.out.println(s2);//输出结果为DJn }</pre>n
结果:
常用方法
方法
解释
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方法演示
<pre class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n String s = "DJTU,China,LiaoNing,DaLian";n String[] strs = s.split(",");//以,分割n for (int i = 0; i < strs.length; i++) {n System.out.println(strs[i]);n }n }</pre>n
结果:
toCharArray方法演示
<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n String s = "DJTU";n char[] chars = s.toCharArray();n for (int i = 0; i < chars.length; i++) {n System.out.println(chars[i]);n }n }</pre>n
结果:
getBytes方法演示
<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n String s = "DJTU";n byte[] bytes = s.getBytes();//按照ASC码表转换为数字n for (int i = 0; i < bytes.length; i++) {n System.out.println(bytes[i]);n }n }</pre>n
结果:
substring方法演示
<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n String s = "DJTU";n String substring = s.substring(1);//从第[1]个开始截取n System.out.println(substring);n }</pre>n
结果:
<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n String s = "DJTU";n String substring = s.substring(1,2);//从第[1]个开始到第[2]个结束(不包含第[2]个)n System.out.println(substring);n }</pre>n
equals方法演示
<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {n String s = "DJTU";n String s2 = "DJTU";n String s3 = "DJTUD";n boolean flag = s.equals(s2);n boolean flag1 = s.equals(s3);n System.out.println(flag);//输出truen System.out.println(flag1);//输出falsen }</pre>n
结果:
特点
- 一个字符串一旦创建其内容是永远不会变的
- 字符串效果上相当于是char[]字符数组,但是底层其实是byte[]字节数组
来源:https://www.cnblogs.com/kohler21/p/16436767.html