当前位置:网站首页>String的trim()和substring()详解
String的trim()和substring()详解
2022-07-01 16:45:00 【Michael_.】
trim()方法:
举例:
String str = " 32143243 2 ";
System.out.println(str.trim());输出(去除了开头和结尾的空格):
我们来看源码进行分析

如果一个字符串开头部分或者结尾部分有空格,则调用substring()将其截掉;
如果没有空格,则返回该字符串
substring()方法:
举例:
String str = "321432432";
System.out.println(str.substring(0,3));输出(截取了下标0-3的子字符串):
查看源码进行分析

当输入的开始索引与结束索引不符合条件时,抛出异常;
当输入的开始索引=0并且结束索引等于字符串长度时,返回原字符串;
否则new一个String对象(地址发生改变)
如果substring()方法中只写一个参数:
String str = "321432432";
System.out.println(str.substring(3));输出(截取了下标3到末尾的数字):
查看源码进行分析:
只写一个开始索引时:
小于0,不符合条件,抛出异常;
开始索引与字符串长度进行比较,差值如果小于0,抛出异常;
差值如果大于0,则以这个索引为开始索引,字符串长度为结束索引new一个String对象
边栏推荐
- 【C語言補充】判斷明天是哪一天(明天的日期)
- Transition technology from IPv4 to IPv6
- 越来越多地使用 SLO 来实现可观测性|DevOps
- 存在安全隐患 起亚召回部分K3新能源
- 【PyG】文档总结以及项目经验(持续更新
- 阿里云、追一科技抢滩对话式AI
- Template Engine Velocity Foundation
- How to restore the system with one click on Lenovo laptop
- SystemVerilog structure (II)
- How to solve the keyboard key failure of notebook computer
猜你喜欢

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

GameFramework食用指南

6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单

重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标

多线程并发之CountDownLatch阻塞等待

【PyG】文档总结以及项目经验(持续更新

英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放

免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!

C语言输入/输出流和文件操作

Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
随机推荐
Bugku's file contains
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
mysql -- explain性能优化
Rhcsa Road
[wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
Hi Fun Summer, play SQL planner with starrocks!
在MeterSphere接口测试中如何使用JMeter函数和MockJS函数
libcurl下载文件的代码示例
What is the effect of choosing game shield safely in the game industry?
How to repair the laptop that cannot connect to the wireless network
英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
redis -- 数据类型及操作
Concatenate strings to get the result with the smallest dictionary order
How to restore the system of Sony laptop
智能运维实战:银行业务流程及单笔交易追踪
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
Dataframe gets the number of words in the string
How to use etcd to realize distributed /etc directory
SQL question brushing 584 Looking for user references