三、String类的获取功能

来源:互联网 发布:在线c语言编译 编辑:程序博客网 时间:2024/05/29 06:53
/* * 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());System.out.println("----------------------");// char charAt(int index):获取指定索引位置的字符System.out.println("charAt:" + s.charAt(7));System.out.println("----------------------");// int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。System.out.println("indexOf:" + s.indexOf('l'));System.out.println("----------------------");// int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。System.out.println("indexOf:" + s.indexOf("owo"));System.out.println("----------------------");// int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。System.out.println("indexOf:" + s.indexOf('l', 4));System.out.println("indexOf:" + s.indexOf('k', 4)); // -1System.out.println("indexOf:" + s.indexOf('l', 40)); // -1System.out.println("----------------------");// 自己练习:int indexOf(String str,int// fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。// String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引System.out.println("substring:" + s.substring(5));System.out.println("substring:" + s.substring(0));System.out.println("----------------------");// String substring(int start,int// end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引System.out.println("substring:" + s.substring(3, 8));System.out.println("substring:" + s.substring(0, s.length()));}}

/* * 需求:遍历获取字符串中的每一个字符 *  * 分析: * A:如何能够拿到每一个字符呢? * char charAt(int index) * B:我怎么知道字符到底有多少个呢? * int length() */public class StringTest {public static void main(String[] args) {// 定义字符串String s = "helloworld";// 原始版本// System.out.println(s.charAt(0));// System.out.println(s.charAt(1));// System.out.println(s.charAt(2));// System.out.println(s.charAt(3));// System.out.println(s.charAt(4));// System.out.println(s.charAt(5));// System.out.println(s.charAt(6));// System.out.println(s.charAt(7));// System.out.println(s.charAt(8));// System.out.println(s.charAt(9));// 只需要我们从0取到9// for (int x = 0; x < 10; x++) {// System.out.println(s.charAt(x));// }// 如果长度特别长,我不可能去数,所以我们要用长度功能for (int x = 0; x < s.length(); x++) {// char ch = s.charAt(x);// System.out.println(ch);// 仅仅是输出,我就直接输出了System.out.println(s.charAt(x));}}}
/* * 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符) * 举例: * "Hello123World" * 结果: * 大写字符:2个 * 小写字符:8个 * 数字字符:3个 *  * 分析: * 前提:字符串要存在 * A:定义三个统计变量 * bigCount=0 * smallCount=0 * numberCount=0 * B:遍历字符串,得到每一个字符。 * length()和charAt()结合 * C:判断该字符到底是属于那种类型的 * 大:bigCount++ * 小:smallCount++ * 数字:numberCount++ *  * 这道题目的难点就是如何判断某个字符是大的,还是小的,还是数字的。 * ASCII码表: * 048 * A65 * a97 * 虽然,我们按照数字的这种比较是可以的,但是想多了,有比这还简单的 * char ch = s.charAt(x); *  * if(ch>='0' && ch<='9') numberCount++ * if(ch>='a' && ch<='z') smallCount++ * if(ch>='A' && ch<='Z') bigCount++ *D:输出结果。 * * 练习:把给定字符串的方式,改进为键盘录入字符串的方式。 */public class StringTest2 {public static void main(String[] args) {//定义一个字符串String s = "Hello123World";//定义三个统计变量int bigCount = 0;int smallCount = 0;int numberCount = 0;//遍历字符串,得到每一个字符。for(int x=0; x<s.length(); x++){char ch = s.charAt(x);//判断该字符到底是属于那种类型的if(ch>='a' && ch<='z'){smallCount++;}else if(ch>='A' && ch<='Z'){bigCount++;}else if(ch>='0' && ch<='9'){numberCount++;}}//输出结果。System.out.println("大写字母"+bigCount+"个");System.out.println("小写字母"+smallCount+"个");System.out.println("数字"+numberCount+"个");}}

0 0
原创粉丝点击