当前位置:网站首页>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
边栏推荐
- 巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
- Iommu/smmuv3 code analysis (10) page table operation
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- 如何使用 etcd 实现分布式 /etc 目录
- Dataframe gets the number of words in the string
- What is the effect of choosing game shield safely in the game industry?
- SQL question brushing 586 Customers with the most orders
- Jojogan practice
- 软件工程导论——第六章——详细设计
- C语言输入/输出流和文件操作
猜你喜欢

Redis 分布式鎖

Mysql database - Advanced SQL statement (2)

Leetcode 77 combination -- backtracking method

Iommu/smmuv3 code analysis (10) page table operation

How to use F1 to F12 correctly on laptop keyboard

How to repair the laptop that cannot connect to the wireless network

Machine learning 11 clustering, outlier discrimination

剑指 Offer 20. 表示数值的字符串

机器学习11-聚类,孤立点判别

Gold, silver and four want to change jobs, so we should seize the time to make up
随机推荐
中国乙腈市场预测与战略咨询研究报告(2022版)
【flask入门系列】Cookie与Session
Installation and use of sqoop
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
整形数组合并【JS】
Machine learning 11 clustering, outlier discrimination
MySQL learning summary
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
Please, stop painting star! This has nothing to do with patriotism!
越来越多地使用 SLO 来实现可观测性|DevOps
In aks, use secret in CSI driver mount key vault
SQL注入漏洞(Mysql与MSSQL特性)
Soft test network engineer full truth simulation question (including answer and analysis)
【Kotlin】高阶函数介绍
There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds
[Supplément linguistique c] déterminer quel jour est demain (date de demain)
String的trim()和substring()详解
Determine whether the linked list is a palindrome linked list
Template Engine Velocity Foundation
中国生物降解塑料市场预测与投资战略报告(2022版)