博客
关于我
String类常用
阅读量:209 次
发布时间:2019-02-28

本文共 1175 字,大约阅读时间需要 3 分钟。

Java String 类方法参考

Java String 类包含多种常用方法,用于字符串操作。以下是主要方法的说明:

1. charAt(int index)

返回指定索引处的字符。

索引范围:0 到 length() - 1
返回值:指定索引处的字符。

2. compareTo(Object o)

比较字符串与对象。

参数Object o
返回值

  • 0:字符串相等。
  • 小于 0:当前字符串小于参数。
  • 大于 0:当前字符串大于参数。

3. compareTo(String anotherString)

比较两个字符串。

参数String anotherString
返回值:同上。

4. indexOf(int ch)

查找字符的第一次出现索引。

参数int ch
返回值:索引或 -1。

5. indexOf(int ch, int fromIndex)

从指定索引开始查找字符。

参数int chint fromIndex
返回值:索引或 -1。

6. indexOf(String str)

查找子字符串的第一次出现索引。

参数String str
返回值:索引或 -1。

7. indexOf(String str, int fromIndex)

从指定索引开始查找子字符串。

参数String strint fromIndex
返回值:索引或 -1。

8. split(String regex, int limit)

拆分字符串。

参数String regexint limit
返回值:字符串数组或原字符串。

9. substring(int beginIndex)

提取子字符串(从起始索引开始,到末尾)。

参数int beginIndex
返回值:子字符串。

10. substring(int beginIndex, int endIndex)

提取子字符串(从起始索引到结束索引,不包括结束索引)。

参数int beginIndexint endIndex
返回值:子字符串。

11. toCharArray()

将字符串转换为字符数组。

返回值char[]

12. trim()

去除首尾空白符。

返回值:去除空白后的字符串。

13. replace(char oldChar, char newChar)

替换字符。

参数char oldCharchar newChar
返回值:替换后的新字符串。

14. getBytes(String charsetName)

将字符串编码为字节序列。

参数String charsetName
返回值byte[]

15. getBytes()

使用默认字符集编码字符串。

返回值byte[]

转载地址:http://bfss.baihongyu.com/

你可能感兴趣的文章
Non-final field ‘code‘ in enum StateEnum‘
查看>>
none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
查看>>
None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
查看>>
NOPI读取Excel
查看>>
NoSQL&MongoDB
查看>>
NoSQL介绍
查看>>
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>