当前位置:网站首页>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对象
边栏推荐
- Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
- [Supplément linguistique c] déterminer quel jour est demain (date de demain)
- 阿里云、追一科技抢滩对话式AI
- Basic use of MySQL
- Rhcsa Road
- 拼接字符串,得到字典序最小的结果
- 今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!
- 中国乙腈市场预测与战略咨询研究报告(2022版)
- 走进微信小程序
- ShenYu 网关开发:在本地启用运行
猜你喜欢
![[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment](/img/0e/52e37527bc717c7a55741725087bad.png)
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment

Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores

Jojogan practice

How to maintain the laptop battery

独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作

Iommu/smmuv3 code analysis (10) page table operation

ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享

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

String class

智能运维实战:银行业务流程及单笔交易追踪
随机推荐
Research and investment strategy report of China's sodium sulfate industry (2022 Edition)
中国PBAT树脂市场预测及战略研究报告(2022版)
荣威 RX5 的「多一点」产品策略
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
Computed property “xxx“ was assigned to but it has no setter.
How to cancel automatic search and install device drivers for laptops
Redis6.0 new features
[C language foundation] 12 strings
Leetcode records - sort -215, 347, 451, 75
阿里云、追一科技抢滩对话式AI
SystemVerilog structure (II)
【C语言补充】判断明天是哪一天(明天的日期)
Redis6.0 新功能
Bugku's file contains
Rhcsa Road
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
SQL question brushing 586 Customers with the most orders
Research and investment strategy report of hydroxypropyl beta cyclodextrin industry in China (2022 Edition)
Soft test software designer full truth simulation question (including answer analysis)
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)