当前位置:网站首页>String类中的常用方法
String类中的常用方法
2022-07-04 22:21:00 【crazyK.】
方法
charAt(int index) | 返回 char 指定索引处的值。 |
---|---|
codePointAt(int index) | 返回指定索引处的字符(Unicode代码点) |
codePointBefore(int index) | 返回指定索引之前的字符(Unicode代码点)。 |
concat(String str) | 将指定的字符串连接到该字符串的末尾。 |
contains(CharSequence s) | 当且仅当此字符串包含指定的char值序列时才返回true。 |
endsWith(String suffix) | 测试此字符串是否以指定的后缀结尾。 |
equals(Object anObject) | 将此字符串与指定对象进行比较。 |
hashCode() | 返回此字符串的哈希码 |
indexOf(String str, int fromIndex) | 返回指定子串的第一次出现的字符串内的索引,以指定的索引开始搜索。 formIndex可以不传 |
isEmpty() | 返回 true 如果,且仅当 length() 为 0 。 |
lastIndexOf(String str, int fromIndex) | 返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向后搜索。 formIndex可以不传 |
length() | 返回此字符串的长度 |
replace(char oldChar, char newChar) | 返回从替换所有出现的导致一个字符串 oldChar 在此字符串 newChar |
replace(CharSequence target, CharSequence replacement) | 将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列。 |
split(String regex) | 将此字符串分割为给定的 regular expression的匹配。 |
startsWith(String prefix) | 测试此字符串是否以指定的前缀开头。 |
substring(int beginIndex, int endIndex) | 返回一个字符串,该字符串是此字符串的子字符串。 endIndex可以不传 |
toCharArray() | 将此字符串转换为新的字符数组。 |
toLowerCase() | 将所有在此字符 String 使用默认语言环境的规则,以小写。 |
toUpperCase() | 将所有在此字符 String 使用默认语言环境的规则大写。 |
使用样例
public class StringTest {
private static String str = "1,a,2,b,3,c";
public static void main(String[] args) {
System.out.println("charAt: "+ str.charAt(0));
System.out.println("codePointAt: " + str.codePointAt(0));
System.out.println("codePointBefore: " + str.codePointBefore(3));
System.out.println("concat: " + str.concat(","));
System.out.println("contains: " + str.contains(","));
System.out.println("endsWith: " + str.endsWith(","));
System.out.println("equals: " + str.equals("ss"));
System.out.println("hashCode: " + str.hashCode());
System.out.println("indexOf: " + str.indexOf(",", 2));
System.out.println("isEmpty: " + str.isEmpty());
System.out.println("lastIndexOf: " + str.lastIndexOf(","));
System.out.println("length: " + str.length());
System.out.println("replace1: " + str.replace("a", "A"));
System.out.println("replace2: " + str.replace("a,2,b", "A,2,B"));
String[] split = str.split(",");
for (String s : split) {
System.out.print(s);
}
System.out.println();
System.out.println("startsWith: " + str.startsWith("1"));
System.out.println("substring: " + str.substring(3, 5));
char[] chars = str.toCharArray();
for (char aChar : chars) {
System.out.print(aChar);
}
System.out.println();
System.out.println("toUpperCase: " + str.toUpperCase());
System.out.println("toLowerCase: " + str.toUpperCase().toLowerCase());
}
}
控制台打印
charAt: 1
codePointAt: 49
codePointBefore: 97
concat: 1,a,2,b,3,c,
contains: true
endsWith: false
equals: false
hashCode: 713949408
indexOf: 3
isEmpty: false
lastIndexOf: 9
length: 11
replace1: 1,A,2,b,3,c
replace2: 1,A,2,B,3,c
1a2b3c
startsWith: true
substring: ,2
1,a,2,b,3,c
toUpperCase: 1,A,2,B,3,C
toLowerCase: 1,a,2,b,3,c
边栏推荐
- 将QA引入软件开发生命周期是工程师要遵循的最佳实践
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
- UML图记忆技巧
- Attack and Defense World MISC Advanced Area Erik baleog and Olaf
- 9 - 类
- 高中物理:直线运动
- leetcode 72. Edit Distance 编辑距离(中等)
- Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
- Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
- 攻防世界 MISC 进阶区 3-11
猜你喜欢
PMO: compare the sample efficiency of 25 molecular optimization methods
都说软件测试很简单有手就行,但为何仍有这么多劝退的?
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
攻防世界 MISC 高手进阶区 001 normal_png
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
LOGO特訓營 第三節 首字母創意手法
LOGO special training camp section I identification logo and Logo Design Ideas
页面关闭前,如何发送一个可靠请求
Logo special training camp Section V font structure and common design techniques
攻防世界 misc 进阶区 2017_Dating_in_Singapore
随机推荐
LOGO特训营 第一节 鉴别Logo与Logo设计思路
Redis的持久化机制
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
How to send a reliable request before closing the page
BigFilter全局交易防重组件的介绍与应用
繁華落盡、物是人非:個人站長該何去何從
蓝队攻防演练中的三段作战
High school physics: linear motion
高中物理:直线运动
LOGO特训营 第四节 字体设计的重要性
剑指 Offer 65. 不用加减乘除做加法
Microservices -- Opening
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
MySQL Architecture - user rights and management
php短视频源码,点赞时会有大拇指动画飘起
环境加密技术解析
PostgreSQL server programming aggregation and grouping
Attack and defense world misc advanced area can_ has_ stdio?
MYSQL架构——逻辑架构