当前位置:网站首页>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)
- 【C补充】【字符串】按日期排序显示一个月的日程
- Rhcsa Road
- ACL 2022 | 分解的元学习小样本命名实体识别
- Leetcode records - sort -215, 347, 451, 75
- Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
- How to cancel automatic search and install device drivers for laptops
- Redis distributed lock
- Leetcode 216 combined summation III -- backtracking method
- 《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
猜你喜欢

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

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

Template Engine Velocity Foundation

PR basic clip operation / video export operation

Shenyu gateway development: enable and run locally

ACL 2022 | 分解的元学习小样本命名实体识别

Flux d'entrées / sorties et opérations de fichiers en langage C

软件工程导论——第六章——详细设计

Template engine velocity Foundation

Cookies and session keeping technology
随机推荐
FRP intranet penetration, reverse proxy
你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
Redis6.0 新功能
How to use F1 to F12 correctly on laptop keyboard
sql刷题584. 寻找用户推荐人
GameFramework食用指南
【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
C語言輸入/輸出流和文件操作
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
P2592 [zjoi2008] birthday party (DP)
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Rhcsa Road
Basic usage of Frida
sql刷题627. 变更性别
How to repair the laptop that cannot connect to the wireless network
【C語言補充】判斷明天是哪一天(明天的日期)
走进微信小程序
Machine learning 11 clustering, outlier discrimination
How to solve the keyboard key failure of notebook computer