当前位置:网站首页>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对象
边栏推荐
- libcurl下载文件的代码示例
- Pytest learning notes (13) -allure of allure Description () and @allure title()
- Soft test network engineer full truth simulation question (including answer and analysis)
- ShenYu 网关开发:在本地启用运行
- Computed property “xxx“ was assigned to but it has no setter.
- Rhcsa Road
- SQL注入漏洞(Mysql与MSSQL特性)
- 判断二叉树是否为二叉搜索树
- [jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
- Hidden Markov model (HMM): model parameter estimation
猜你喜欢
随机推荐
SQL注入漏洞(Mysql与MSSQL特性)
Installation and use of sqoop
[kotlin] Introduction to higher-order functions
Template Engine Velocity Foundation
How wild are hackers' ways of making money? CTF reverse entry Guide
中国酶制剂市场预测与投资战略研究报告(2022版)
单例模式的懒汉模式跟恶汉模式的区别
How to use etcd to realize distributed /etc directory
挖财学堂班主任给的证券账户安全吗?能开户吗?
Advantages, values and risks of chain games compared with traditional games
Detailed explanation of activity life cycle and startup mode
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
判断一棵二叉树是否为平衡二叉树
剑指 Offer II 015. 字符串中的所有变位词
Iommu/smmuv3 code analysis (10) page table operation
String类
China sorbitol Market Forecast and investment strategy report (2022 Edition)
【C语言基础】12 字符串
redis -- 数据类型及操作
China BMS battery management system Market Research Report (2022 Edition)







