当前位置:网站首页>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对象
边栏推荐
- AI college entrance examination volunteer filling: the gods of Dachang fight, and candidates pay to watch
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
- SQL question brushing 584 Looking for user references
- 判断链表是否是回文链表
- Activity的生命周期和启动模式详解
- 【Kotlin】高阶函数介绍
- 你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
- 软件工程导论——第六章——详细设计
- Hi Fun Summer, play SQL planner with starrocks!
- Jojogan practice
猜你喜欢
Machine learning 11 clustering, outlier discrimination
SQL question brushing 627 Change gender
Template Engine Velocity Foundation
为什么你要考虑使用Prisma
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
[C language foundation] 12 strings
sql刷题584. 寻找用户推荐人
软件工程导论——第六章——详细设计
Leetcode 77 combination -- backtracking method
巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
随机推荐
AI高考志愿填报:大厂神仙打架,考生付费围观
越来越多地使用 SLO 来实现可观测性|DevOps
Basic usage of Frida
Basic use of MySQL
SQL question brushing 586 Customers with the most orders
[kotlin] Introduction to higher-order functions
Activity的生命周期和启动模式详解
Bugku's file contains
Leetcode records - sort -215, 347, 451, 75
redis -- 数据类型及操作
软件工程导论——第六章——详细设计
sql刷题1050. 合作过至少三次的演员和导演
Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"
String class
Hidden Markov model (HMM): model parameter estimation
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
Installation and use of sqoop
Rhcsa Road
SQL注入漏洞(Mysql与MSSQL特性)
Redis 分布式锁