目录

概述

字符串广泛应用 在 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. PE Header中的FIleHeader(文件头)
  2. 白话Elasticsearch13-深度探秘搜索技术之基于multi_match+most fields策略进行multi-field搜索
  3. 小样本学习 | Learning to Compare: Relation Network for Few-Shot Learning
  4. 第四篇 Python循环
  5. 大师兄科研网vasp_怎样知道一名研究生有没有科研潜力?
  6. istio 和 kong_如何启动和运行Istio
  7. vue本地下载文件,解决ie浏览器本地下载文件无反应(已解决);vue-cli2本地下载文件,vue-cli3本地下载文件
  8. 为什么当前互联网+法律发展艰难?
  9. 3.1 神经网络概览
  10. 一个500强公司的数据化运营管理实践
  11. 我的世界服务器显示红心,我的世界手机版红心怎么恢复 | 手游网游页游攻略大全...
  12. 常见的反爬策略及解决方案
  13. 机器学习初探:(十一)主成分分析
  14. div和span标签以及标签分类
  15. 国内云桌面架构有哪些?为什么VDI能成为主流
  16. 让你越来越值钱的秘密:目标清单
  17. 防护器件-ESD管知识普及
  18. PHP CURL Authorization: Basic 获取token
  19. html翻译插件,vscode系列: 做个五脏俱全的翻译插件
  20. 【手册】如何编译/修改三星手机Rom(三)

热门文章

  1. cmds系统归并缓慢的处理过程 2017-2-16
  2. c++语言程序设计(郑莉)学习笔记(详细中的详细)
  3. 操作系统形式化验证实践教程(11) - 结构化证明语言Isar
  4. Flutter引入iconfont图标,并自动生成dart图标文件
  5. 数学分析教程史济怀练习8.4
  6. tcp服务器维护接入客户端mac,tcp服务器怎么获取客户端mac
  7. STM32F103DAC输出直流、锯齿波、三角波,正弦波
  8. C#计算伪逆矩阵(PseudoInverse)
  9. AI与系统软件的深度融合研究进展与趋势(综述)
  10. 员工管理书籍推荐,做好员工管理看这些书就够了