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 }