常见对象-String类-2
来源:互联网 发布:广西广电网络机顶盒ip 编辑:程序博客网 时间:2024/06/03 15:10
package cn.itcast_04;/* * String类的获取功能 * int length():获取字符串的长度。 * char charAt(int index):获取指定索引位置的字符 * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。 * 为什么这里是int类型,而不是char类型? * 原因是:'a'和97其实都可以代表'a' * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。 * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。 * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。 * String substring(int start):从指定位置开始截取字符串,默认到末尾。 * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。 */public class StringDemo {public static void main(String[] args) {// 定义一个字符串对象String s = "helloworld";// int length():获取字符串的长度。System.out.println("s.length:" + s.length()); //s.length:10// char charAt(int index):获取指定索引位置的字符System.out.println("charAt:" + s.charAt(7)); //charAt:r// int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。System.out.println("indexOf:" + s.indexOf('l')); //indexOf:2// int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。System.out.println("indexOf:" + s.indexOf("owo")); //indexOf:4// int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。System.out.println("indexOf:" + s.indexOf('l', 4)); //indexOf:8System.out.println("indexOf:" + s.indexOf('k', 4)); // -1System.out.println("indexOf:" + s.indexOf('l', 40)); // -1// 自己练习:int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。System.out.println("indexOf:" + s.indexOf("rl", 4)); //indexOf:7// String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引System.out.println("substring:" + s.substring(5));//substring:worldSystem.out.println("substring:" + s.substring(0));//substring:helloworld// String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引System.out.println("substring:" + s.substring(3, 8)); //substring:loworSystem.out.println("substring:" + s.substring(0, s.length()));//substring:helloworld}}
0 0
- 常见对象-String类-2
- 常见对象-String类
- 常见对象-String类-3
- 常见对象-String类-4
- 03-常用对象API(String类-常见功能-获取_1) 1 2 3 05-常用对象API(String类-常见功能-转换)1 2 3 4
- 常见对象_Scanner、String总结
- 15-05-常用对象API(String类-常见功能-转换)
- 15-06-常用对象API(String类-常见功能-判断)
- 15-07-常用对象API(String类-常见功能-比较)
- 常见对象-String类的compareTo()方法的源码解析
- 06-常用对象API(String类-常见功能-判断) 07-常用对象API(String类-常见功能-比较)
- 常见类---String类
- string类常见函数
- 15-03-常用对象API(String类-常见功能-获取_1)
- 15-04-常用对象API(String类-常见功能-获取_2)
- 常见的String类问题
- String类常见的方法
- String类常见的使用方法
- 五分钟创建个人公众号并接入图灵机器人
- Eclipse没有server 配置Tomcat
- hdu 1024 Max Sum Plus Plus
- Dubbo架构设计详解
- javatst2
- 常见对象-String类-2
- docker 入门
- FFT变换Android代码及解析
- 软件光栅化渲染器(六)
- Java设计模式之策略模式
- github中的star、watch、fork
- 添加 cmder 到右键菜单
- Web AppBuilder for ArcGIS(开发版)系列教程 3
- HDU