当前位置:网站首页>String中常用的API
String中常用的API
2022-07-28 17:55:00 【剑小麟】
1.length() 表示长度 charAt(i)表示指出某个位置的字符
代码示例如下:
public class Capi {
public static void main(String[] args) {
String name="我爱java";
System.out.println(name.length());//长度
System.out.println(name.charAt(1));//某位置字符
}
}运行结果如下:

2. substring(x,y) 表示截取字段 xy分别表示需要进行的起始和末尾位置
substring(z) 表示从当前位置截取到最后
代码示例如下:
public class Capi {
public static void main(String[] args) {
String name2="我爱java哈哈哈";
System.out.println(name2.substring(0, 6));//截取字段 起始和末尾
System.out.println(name2.substring(5));//当前位置截取到最后
}
}运行结果如下:

3.replace("a","b") 将该字符串中的a替换为b
contains("c") 是否有c字符串
startsWith("d") d字符串是否为开头
代码示例如下:
public class Capi {
public static void main(String[] args) {
String name3="我爱java哈哈哈";
String st=name3.replace("哈哈哈","***");
System.out.println(name3.contains("java"));//是否有该字符段
System.out.println(name3.startsWith("我"));//该字符是否为开头
}
}运行结果如下:

边栏推荐
- leetcode day4 部门工资最高的员工
- 冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题
- 11. Learn MySQL union operator
- Pagoda panel construction novel CMS management system source code measurement - thinkphp6.0
- Scrapy Spider源码分析
- Rust 入门指南(rustup, cargo)
- Prometheus部署
- BLDC 6-step commutation simulink
- 测试开发备忘
- MySQL performance testing tool sysbench learning
猜你喜欢
随机推荐
shared_ PTR and make_ Use of shared
navicate修改数据库名的方式
editor.md中markdown编辑器的实现
NetCoreAPI操作Excel表格
Preliminary learning function (3rd blog)
BLDC 6-step commutation simulink
The mystery of ID number
What parameters should be passed in calling integer or character array functions
Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation
基于C语言的信息管理系统和小游戏
leetcode day5 删除重复的电子邮箱
Convertible bond concept table x notation gives you a convenient and fast experience!
MySQL性能测试工具sysbench学习
The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board
芯片功耗性能验证:从困境到超越
在矩池云快速安装torch-sparse、torch-geometric等包
使用SaltStack自动化部署Zabbix
微信公众号授权登录后报redirect_uri参数错误的问题
How does app automated testing achieve H5 testing
This customized keyboard turns me on~









