目录

概述

字符串广泛应用 在 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, &quot;Courier New&quot;, 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>

使用构造方法

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

String()

public String();

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

示例代码:

<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, 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) {//使用无参构造创建。字符串的内容为空 相当于 ""String s1 = new String();}</pre>

String(byte[] bytes)

String(byte[] bytes);

将数组转换为字符串。

示例代码:

<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, 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) {byte[] bytes = {68,74,84,85};String s = new String(bytes);System.out.println(s);//输出结果为DJTU,这里是将数字通过ASC码表转换为了字母}</pre>

结果:

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, &quot;Courier New&quot;, 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) {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个}</pre>

结果:

String(char[] value)

转换字符数组为字符串类

示例代码:

<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, 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) {char[] chars = {'D','J','T','U'};String s = new String(chars);System.out.println(s);//输出结果为DJTU}</pre>

结果:

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, &quot;Courier New&quot;, 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) {char[] chars = {'D','J','T','U'};String s = new String(chars,0,1);System.out.println(s);//输出结果为DString ss = new String(chars,0,2);System.out.println(s2);//输出结果为DJ}</pre>

结果:

常用方法

方法 解释
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, &quot;Courier New&quot;, 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) {String s = "DJTU,China,LiaoNing,DaLian";String[] strs = s.split(",");//以,分割for (int i = 0; i < strs.length; i++) {System.out.println(strs[i]);}}</pre>

结果:

toCharArray方法演示

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, 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) {String s = "DJTU";char[] chars = s.toCharArray();for (int i = 0; i < chars.length; i++) {System.out.println(chars[i]);}}</pre>

结果:

getBytes方法演示

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, 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) {String s = "DJTU";byte[] bytes = s.getBytes();//按照ASC码表转换为数字for (int i = 0; i < bytes.length; i++) {System.out.println(bytes[i]);}}</pre>

结果:

substring方法演示

<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, 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) {String s = "DJTU";String substring = s.substring(1);//从第[1]个开始截取System.out.println(substring);}</pre>

结果:

<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, 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) {String s = "DJTU";String substring = s.substring(1,2);//从第[1]个开始到第[2]个结束(不包含第[2]个)System.out.println(substring);}</pre>

equals方法演示

<pre class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, 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) {String s = "DJTU";String s2 = "DJTU";String s3 = "DJTUD";boolean flag = s.equals(s2);boolean flag1 = s.equals(s3);System.out.println(flag);//输出trueSystem.out.println(flag1);//输出false}</pre>

结果:

特点

  1. 一个字符串一旦创建其内容是永远不会变的
  2. 字符串效果上相当于是char[]字符数组,但是底层其实是byte[]字节数组

Java String类相关推荐

  1. java --String类解决面试问题

    2019独角兽企业重金招聘Python工程师标准>>> 1.概述 字符串对象是一种特殊的对象.String类是一个不可变的类..也就说,String对象一旦创建就不允许修改 Stri ...

  2. Java——String类的方法

    Java--String类的方法 String str1 = "abc" 与String str2 = new String("abc")有什么区别? 字符串常 ...

  3. java string.substring 参数,Java,String类中的subString()方法,stringsubstring

    Java,String类中的subString()方法,stringsubstring public class TestStringSubString { // main函数 public stat ...

  4. Java String类的相关操作

    Java String类的相关操作 一.如何遍历字符串 //法一 String str="hello world"; for(int i=0;i<str.length();i ...

  5. Java String类的split方法简介

    Java String类的split方法简介 String的split()方法用于按传入的字符或字符串对String进行拆分,返回拆分之后的数组. 1.一般用法 用一般的字符,例如 @ 或 , 等符号 ...

  6. Java String类概述

    Java String类 String类简介 字符串比较 String对象(常量)池 静态常量池 运行时常量池 String类简介 字符串严格意义上来讲并不能算是一个基本数据类型,也就是说没有任何一门 ...

  7. 黑马程序员——Java String类 and 正则表达式(第七篇)

    -----------android培训.java培训.java学习型技术博客.期待与您交流!------------ 虽然老毕视频中把正则表达式放到了最后面才讲,但个人认为他和String功能上有些 ...

  8. Java——String类中的compareTo方法总结

    String类的定义:    java.lang  类 String   java.lang.Object       java.lang.String 所有已实现的接口: Serializable, ...

  9. java string类api_JAVA中String类的常用方法API

    @[toc] 前言 String 类是我们日常经常使用的Java类,以下是对该类的信息汇总,类的关系图如下 String类关系图 创建: String s="hello!";//使 ...

  10. Java学习笔记之:Java String类

    一.引言 字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提供了String类来创建和操作字符串. 创建字符串最简单的方式如下: String str= "Hello w ...

最新文章

  1. 又一所双一流大学明确:发表论文数量不再作为博士毕业的限制性条件
  2. linux 命令读db文件格式,使用linux的db_load命令生成db数据库
  3. adcsr图像超分代码_图像超分:RealSR
  4. oppo设备怎么样无需root激活XPOSED框架的教程
  5. 压缩图片_Word快速压缩图片大小
  6. 玩客云刷windows做服务器_精选 | 搭建一个私人服务器如何?
  7. JSP根据状态动态改变数据表格按钮
  8. 【LuoguP5289】[十二省联考2019] 皮配
  9. Linux 系统中的超级权限的控制
  10. UnderWater+SDN论文之六
  11. C# 小票打印机 直接打印 无需驱动
  12. 数字万用表常用功能使用
  13. 【转】2014阿里巴巴面试经历
  14. 如何禁止Windows自动更新AMD显卡驱动
  15. osx 续航测试软件,苹果吹牛了吗?多款Macbook续航测试结果出炉
  16. 最大流/最小割算法总结
  17. 星辰变最后鸿蒙,星辰变最后的结局是什么
  18. python opencv 读取视频保存视频片段和图片
  19. 谷歌地图卫星下周发射 分辨率提高至0.5米
  20. MRCP 媒体资源控制协议

热门文章

  1. maven创建项目的模版类型(基于3.3.9版本)
  2. 南山驿站机器人_波士顿动力机器人即将商用,可自动避障且会跳舞
  3. 【初识大数据】1、大数据简介
  4. HTTP隧道代理及wireshark抓包分析HTTPS过程
  5. 【python基础】——python 复数运算
  6. 【吐槽贴】项目经理如何进行高效沟通?
  7. 小明Q1投影仪好不好?值不值得买?对比当贝D1如何?
  8. 学习普中的51单片机TFT彩屏篇中遇到的问题和解决办法
  9. 谷歌浏览器插件-html页面js事件查看器
  10. Element-UI安装使用教程(一)