当前位置:网站首页>Detailed explanation of string's trim() and substring()
Detailed explanation of string's trim() and substring()
2022-07-01 17:02:00 【Michael_.】
If substring() Write only one parameter in the method :
trim() Method :
give an example :
String str = " 32143243 2 ";
System.out.println(str.trim()); Output ( Remove the spaces at the beginning and end ):
Let's look at the source code for analysis 

If there are spaces at the beginning or end of a string , Call substring() Cut it off ;
If there is no space , Then return the string
substring() Method :
give an example :
String str = "321432432";
System.out.println(str.substring(0,3)); Output ( Intercepted subscript 0-3 Substring of ):
Check the source code for analysis 

When the input start index and end index do not meet the conditions , Throw an exception ;
When entering the start index =0 And when the end index is equal to the length of the string , Returns the original string ;
otherwise new One String object ( Address change )
If substring() Write only one parameter in the method :
String str = "321432432";
System.out.println(str.substring(3)); Output ( Intercepted subscript 3 Numbers to the end ):
Check the source code for analysis :
When writing only one starting index :
Less than 0, disqualification , Throw an exception ;
Start to compare the index with the string length , If the difference is less than 0, Throw an exception ;
If the difference is greater than 0, Start with this index , The length of the string is the end index new One String object
边栏推荐
- How to use F1 to F12 correctly on laptop keyboard
- 如何使用 etcd 实现分布式 /etc 目录
- 存在安全隐患 起亚召回部分K3新能源
- How to restore the system with one click on Lenovo laptop
- Hi Fun Summer, play SQL planner with starrocks!
- [mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
- What are the differences between PHP and DW
- The difference between the lazy mode of singleton mode and the evil mode
- String的trim()和substring()详解
- 中国茂金属聚乙烯(mPE)行业研究报告(2022版)
猜你喜欢

模板引擎Velocity 基礎

智能运维实战:银行业务流程及单笔交易追踪

SQL question brushing 586 Customers with the most orders

(28) Shape matching based on contour features

为什么你要考虑使用Prisma

Redis6.0 new features

ShenYu 网关开发:在本地启用运行

Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!

【Try to Hack】vulnhub DC4

SQL question brushing 627 Change gender
随机推荐
Research and investment strategy report of China's sodium sulfate industry (2022 Edition)
Installation and use of sqoop
String class
中国超高分子量聚乙烯产业调研与投资前景报告(2022版)
Yyds dry inventory MySQL RC transaction isolation level implementation
判断二叉树是否为二叉搜索树
Redis 分布式锁
多线程并发之CountDownLatch阻塞等待
中国酶制剂市场预测与投资战略研究报告(2022版)
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
求求你们,别再刷 Star 了!这跟“爱国”没关系!
Activity的生命周期和启动模式详解
FRP intranet penetration, reverse proxy
单例模式的懒汉模式跟恶汉模式的区别
可迭代对象与迭代器、生成器的区别与联系
Detailed explanation of activity life cycle and startup mode
Iommu/smmuv3 code analysis (10) page table operation
Please, stop painting star! This has nothing to do with patriotism!
智能运维实战:银行业务流程及单笔交易追踪
How to repair the laptop that cannot connect to the wireless network