Java基础知识强化33:String类之String类的获取功能

1. String类的获取功能

int  length() // 获取字符串中字符的个数(长度)
char  charAt(int index)//根据位置获取字符

int  indexOf(int ch)//获取字符ch在字符串中第一次出现的位置索引
int  indexOf(String  str)//获取str在字符串中第一次出现的位置
int  indexOf(int ch, int fromIndex)//从指定位置from查找ch第一次出现的位置索引
int  indexOf(String str,int fromIndex)//从指定位置from查找字符串str第一次出现的位置索引

int lastIndexOf(int ch)//获取字符ch在字符串中最后出现的位置索引
int lastIndexOf(int ch, int from)//从指定位置from查找ch最后出现的位置索引
int lastIndexOf(String str)//获取str在字符串最后出现的位置
int lastIndexOf(String str, int index)//从指定位置from查找str最后出现位置

//获取字符串的一部分
String substring(int start)
String substring(int start,int end)

 

2. 案例:

 1 package cn.itcast_04;
 2 
 3 /*
 4  * String类的获取功能
 5  * int length():获取字符串的长度 6  * char charAt(int index):获取指定索引位置的字符
 7  * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引 8  *         为什么这里是int类型,而不是char类型?
 9  *         原因是:'a'和97其实都可以代表'a'
10  * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引11  * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引12  * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引13  * String substring(int start):从指定位置开始截取字符串,默认到末尾14  * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串15  */
16 public class StringDemo {
17     public static void main(String[] args) {
18         // 定义一个字符串对象
19         String s = "helloworld";
20 
21         // int length():获取字符串的长度。
22         System.out.println("s.length:" + s.length());
23         System.out.println("----------------------");
24 
25         // char charAt(int index):获取指定索引位置的字符
26         System.out.println("charAt:" + s.charAt(7));
27         System.out.println("----------------------");
28 
29         // int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
30         System.out.println("indexOf:" + s.indexOf('l'));
31         System.out.println("----------------------");
32 
33         // int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
34         System.out.println("indexOf:" + s.indexOf("owo"));
35         System.out.println("----------------------");
36 
37         // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
38         System.out.println("indexOf:" + s.indexOf('l', 4));
39         System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
40         System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
41         System.out.println("----------------------");
42 
43         // 自己练习:int indexOf(String str,int
44         // fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
45 
46         // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
47         System.out.println("substring:" + s.substring(5));
48         System.out.println("substring:" + s.substring(0));
49         System.out.println("----------------------");
50 
51         // String substring(int start,int
52         // end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
53         System.out.println("substring:" + s.substring(3, 8));
54         System.out.println("substring:" + s.substring(0, s.length()));
55     }
56 }

运行结果:

 

3.  小案例

(1)字符串遍历

 1 package cn.itcast_04;
 2 
 3 /*
 4  * 需求:遍历获取字符串中的每一个字符
 5  * 
 6  * 分析:
 7  *         A:如何能够拿到每一个字符呢?
 8  *             char charAt(int index)
 9  *         B:我怎么知道字符到底有多少个呢?
10  *             int length()
11  */
12 public class StringTest {
13     public static void main(String[] args) {
14         // 定义字符串
15         String s = "helloworld";
16 
17         // 原始版本
18         // System.out.println(s.charAt(0));
19         // System.out.println(s.charAt(1));
20         // System.out.println(s.charAt(2));
21         // System.out.println(s.charAt(3));
22         // System.out.println(s.charAt(4));
23         // System.out.println(s.charAt(5));
24         // System.out.println(s.charAt(6));
25         // System.out.println(s.charAt(7));
26         // System.out.println(s.charAt(8));
27         // System.out.println(s.charAt(9));
28 
29         // 只需要我们从0取到9
30         // for (int x = 0; x < 10; x++) {
31         // System.out.println(s.charAt(x));
32         // }
33 
34         // 如果长度特别长,我不可能去数,所以我们要用长度功能
35         for (int x = 0; x < s.length(); x++) {
36             // char ch = s.charAt(x);
37             // System.out.println(ch);
38             // 仅仅是输出,我就直接输出了
39             System.out.println(s.charAt(x));
40         }
41     }
42 }

(2)统计大小写字母、数字字符的个数

分析:• 定义三个统计变量:bigCount = 0,smallCount = 0,numberCount = 0

         • 遍历字符串,得到每一个字符:length() 和 charAt()结合

         • 判断该字符到底是属于那种类型的:

                     大写:bigCount++

                     小写:smallCount++

                     数字:numberCount++

 1 package cn.itcast_04;
 2 
 3 /*
 4  * 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
 5  * 举例:
 6  *         "Hello123World"
 7  * 结果:
 8  *         大写字符:2个
 9  *         小写字符:8个
10  *         数字字符:3个
11  * 
12  * 分析:
13  *         前提:字符串要存在
14  *         A:定义三个统计变量
15  *             bigCount=0
16  *             smallCount=0
17  *             numberCount=0
18  *         B:遍历字符串,得到每一个字符。
19  *             length()和charAt()结合
20  *         C:判断该字符到底是属于那种类型的
21  *             大:bigCount++
22  *             小:smallCount++
23  *             数字:numberCount++
24  * 
25  *             这道题目的难点就是如何判断某个字符是大的,还是小的,还是数字的。
26  *             ASCII码表:
27  *                 0    48
28  *                 A    65
29  *                 a    97
30  *             虽然,我们按照数字的这种比较是可以的,但是想多了,有比这还简单的
31  *                 char ch = s.charAt(x);
32  * 
33  *                 if(ch>='0' && ch<='9') numberCount++
34  *                 if(ch>='a' && ch<='z') smallCount++
35  *                 if(ch>='A' && ch<='Z') bigCount++
36  *        D:输出结果。
37  *
38  * 练习:把给定字符串的方式,改进为键盘录入字符串的方式。
39  */
40 public class StringTest2 {
41     public static void main(String[] args) {
42         //定义一个字符串
43         String s = "Hello123World";
44         
45         //定义三个统计变量
46         int bigCount = 0;
47         int smallCount = 0;
48         int numberCount = 0;
49         
50         //遍历字符串,得到每一个字符。
51         for(int x=0; x<s.length(); x++){
52             char ch = s.charAt(x);
53             
54             //判断该字符到底是属于那种类型的
55             if(ch>='a' && ch<='z'){
56                 smallCount++;
57             }else if(ch>='A' && ch<='Z'){
58                 bigCount++;
59             }else if(ch>='0' && ch<='9'){
60                 numberCount++;
61             }
62         }
63         
64         //输出结果。
65         System.out.println("大写字母"+bigCount+"个");
66         System.out.println("小写字母"+smallCount+"个");
67         System.out.println("数字"+numberCount+"个");
68     }
69 }

 运行结果:

posted on 2015-09-19 15:31  鸿钧老祖  阅读(221)  评论(0编辑  收藏  举报

导航