java练习题之String字符串

1 编写程序将"jdk" 全部变成大写  并输出到屏幕 截取”DK“并输出到屏幕

 1 package com.lv.study.am.first;
 2 
 3 public class DemoString {
 4 
 5     public static void main(String[] args) {
 6 
 7         test1();
 8         
 9     }
10     
11     //问题驱动教学
12     //
13 1 编写程序将"jdk" 全部变成大写  并输出到屏幕 截取”DK“并输出到屏幕
14     public static void test1(){
15         
16         String str1="jdk";
17         
18         //1 将jdk全部变成大写
19         
20         System.out.println(str1.toUpperCase());
21         
22         
23         /*
24          * String substring(int beginIndex)返回一个新的字符串,他是此字符串的一个字符串
25          * toCharArray()
26          * replace()
27          * 
28          */
29         //2 截取DK 并打印
30         //方法1
31         System.out.println(str1.substring(1).toUpperCase());//从指定下标开始截取子串
32         
33         //方法2
34         char[] ca=str1.toCharArray();//讲我们的字符串转变成一个字符数组
35         for(int i=1;i<ca.length;i++){
36             System.out.print((char)(ca[i]-32));
37         }
38         
39         System.out.println();
40         //方法3
41         System.out.println(str1.replace('j', ' ').trim().toUpperCase());
42         
43     }
44     
45 }

 2 统计我们一个字符串里面出现的字母 出现的其他符号各有多少个

 1 package com.lv.study.pm.first;
 2 
 3 /*
 4  * toCharArray
 5  * toUpperCase
 6  * toLowerCase
 7  */
 8 public class DemoString {
 9 
10     public static void main(String[] args) {
11 
12         //test2();
13         test3();
14         
15     }
16     
17     // 2 统计我们一个字符串里面出现的字母 出现的其他符号各有多少个
18     public static void test3(){
19         
20         String name="abc123+++kkk";
21         char[] ca=name.toCharArray();
22         
23         //统计字符,数字,特殊字符
24         int countChar=0;
25         int countNumber=0;
26         int other=0;
27         
28         for(int i=0;i<ca.length;i++){
29             
30             //if(ca[i]>='0'&&ca[i]<='9'){
31             if(ca[i]>=48&&ca[i]<=57){
32                 countNumber++;
33             }else if((ca[i]>='A' && ca[i]<='Z')||(ca[i]>='a' && ca[i]<='z')){
34                 countChar++;
35             }else{
36                 other++;
37             }
38         }
39         
40         System.out.println("字母的个数"+countChar);
41         System.out.println("数字的个数"+countNumber);
42         System.out.println("字母的个数"+other);
43     }
44     
45     public static void test2(){
46 
47         //能不能将字符串的大写变成小写 小写变成大写
48         String name="jsFJGsjsj";
49         
50         //1.1 讲字符串变成一个字符数组
51         char[] ca=name.toCharArray();
52         
53         //1.2 要将字符数组里面每一个字母进行大小写转换
54         //  大写 +32变成小写   小写-32变成大写
55         
56         for(int i=0;i<ca.length;i++){
57             //判断是大写还是小写
58             if(ca[i]>='A' && ca[i]<='Z'){
59                 //说明我们的字符串就是大写字母
60                 ca[i]+=32;//变成小写
61             }else if(ca[i]>='a' && ca[i]<='z'){
62                 //说明我们的字符串就是小写字母
63                 ca[i]-=32;//变成大写
64             }
65         }
66         
67         for(char c:ca){
68             System.out.print(c);
69         }
70         
71         
72     }
73     
74     public static void test1(){
75         String str="hhsssDDSRTss";
76         //将字符串的所有字母变成大写
77         String strc=str.toUpperCase();
78         System.out.println("变成大写以后"+strc);
79         
80         //将字符串的所有字母变成小写
81         String strlc=str.toLowerCase();
82         System.out.println("全部变成小写以后"+strlc);
83     }
84 }

3 请查找a字符串在b字符串里面出现过多少次

  1 package com.lv.study.pm.first;
  2 
  3 /*
  4  * contains  字符串a是否包含字符串b
  5  * length  返回字符串的长度
  6  * indexOf 查找这个字符串出现的第一次下标的位置
  7  * lastindexOf 查找这个字符串出现的最后一次下标的位置
  8  * substring(beginindex)
  9  * substring(beginindex,endindex)截取起始下标(包含) 终止下标的位置(不包含)  
 10  * 
 11  * 
 12  * 
 13  */
 14 public class DemoStringSub {
 15 
 16     public static void main(String[] args) {
 17 
 18 //        test1();
 19 //        
 20 //        if(test2("","")){
 21 //            System.out.println("包含");
 22 //        }else{
 23 //            System.out.println("不包含");
 24 //        }
 25 //        
 26 //        
 27 //        int index=test3("","");
 28 //        if(index>-1){
 29 //            System.out.println("包含,位置在"+index);
 30 //        }else{
 31 //            System.out.println("不包含");
 32 //        }
 33 //        
 34         
 35 //        int index=test4("","");
 36 //        if(index>-1){
 37 //            System.out.println("包含,最后一次位置在"+index);
 38 //        }else{
 39 //            System.out.println("不包含");
 40 //        }
 41 
 42         test5();
 43     }
 44         
 45     
 46     //请查找a字符串在b字符串里面出现过多少次
 47     public static void test5(){
 48         
 49         String a="ab";
 50         String b="abhablkjabcab";
 51         
 52         //找到第一个字符串的下标+要查找的字符串的长度  下一个开始查找的字符串的起始位置
 53         //出现多少次
 54         ch(b,a);
 55         System.out.println("出现的次数"+number);
 56     }
 57     
 58     static int number=0;//出现的次数
 59     
 60     // a 查找的目标
 61     // b 需要查找的字符串
 62     private static  String ch(String a,String b){
 63         //去a字符串 查找b是否存在  如果不存在 就是方法的出口
 64         if(a.indexOf(b)==-1){
 65             return null;//代表我们已经到了方法的出口
 66         }
 67         number++;//表示找到一次
 68         
 69         //a.indexOf(b)  代表找到了b字符串所在的下标+b字符串的长度 就是下一个字符串需要查找的下标
 70         int index=a.indexOf(b)+b.length();//就是下一个字符串的起始位置
 71         //String suba=a.substring(index);//就是我们下一次要查找的目标字符串
 72          a=a.substring(index);
 73         return ch(a,b);
 74     }
 75     
 76     
 77     
 78     //请查找字符串a是否在字符串b里面是否存在 并且要知道他最后一次出现的下标是多少
 79             public static int test4(String a,String b){
 80                 
 81                 int index=-1;//默认不包含存在
 82                 
 83                 a="ja";
 84                 b="abjkshsaiabdahsabbsjabsjabsjasjhsaiabdahsabsjajhsaiabdahsabsjajajjabacsh";
 85                 
 86                 index=b.lastIndexOf(a);//从b里面去找他的身体里面是不是含有a  如果存在就返回最后一次出现的下标
 87                 
 88                 return index;
 89                 
 90             }
 91     
 92     
 93     //请查找字符串a是否在字符串b里面是否存在 并且要知道他的下标是多少
 94         public static int test3(String a,String b){
 95             
 96             int index=-1;//默认不包含存在
 97             
 98             a="ja";
 99             b="abjkshsaiabdahsabsjajjabacsh";
100             
101             index=b.indexOf(a);//从b里面去找他的身体里面是不是含有a  如果存在就返回第一次出现的下标
102             return index;
103             
104         }
105     
106     //请查找字符串a是否在字符串b里面是否存在
107     public static boolean test2(String a,String b){
108         
109         //判断字符串a是否在字符串b里面是否存在
110         boolean flag=true;//默认是存在
111         
112         a="abc";
113         b="abjkshsaiabdahsabsjajjabacsh";
114         
115         flag=b.contains(a);//多态的应用jdk
116         
117         return flag;
118         
119     }
120 
121     //1 请截取 thank you 出来
122     //1.1 找到thank you 的下标
123     //1.2 根据指定的下标截取字符串
124     public static void test1(){
125         
126         String name="i  am  lvchang  today  sunday thank you";
127         
128         int index=name.indexOf("thank you");//找到目标字符串里面的参数字符串所在的下标  没找到返回-1
129         System.out.println(index);//参数字符串,在目标字符串的哪一个下标
130         
131         //从起始下标开始,截取一个字符串
132         String subname=name.substring(index);
133         System.out.println(subname);
134         
135     }
136     
137 }

猜你喜欢

转载自www.cnblogs.com/dabu/p/12407599.html