当前位置:网站首页>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("我"));//该字符是否为开头
}
}运行结果如下:

边栏推荐
- 助力面板行业创新,FPGA将成屏厂TCON最佳选择?
- 华为入股南京芯视界,布局固态激光雷达芯片领域
- 英文翻译西班牙语-批量英文翻译西班牙工具免费
- Rust Getting Started Guide (modules and engineering structures)
- Redis笔记
- 2022年全国最新消防设施操作员(中级消防设施操作员)题库及答案
- Smart contract security - overflow vulnerability
- 你知道雨的类型有几种?
- lua语言的左对齐函数(手写)
- Cvpr19 - adjust reference dry goods bag of tricks for image classification with revolutionary neural network
猜你喜欢
随机推荐
架构基本概念和架构本质
High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
芯片功耗性能验证:从困境到超越
第一次写博客
宝塔面板搭建小说CMS管理系统源码实测 - ThinkPHP6.0
Investment of 3.545 billion yuan! Gree Group participates in Xiaomi industry fund
Business visualization - let your flowchart "run" (4. Actual business scenario test)
shared_ptr 和 make_shared 的使用
串口接收应用——环形缓冲buffer
Rust Getting Started Guide (rustup, cargo)
Amazon launched Amazon one palm payment system, and the contactless palm vein recognition market is expected to explode
C language cycle sentence strengthening exercises
Verification of chip power consumption performance: from dilemma to transcendence
英文翻译西班牙语-批量英文翻译西班牙工具免费
The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board
Leetcode day2 连续出现的数字
Rust 入门指南(modules 和工程结构)
Search problems and technologies
时间转日期的sql语句应该怎么写?
Convertible bond concept table x notation gives you a convenient and fast experience!









