標籤:span [] fse class 定義 substring 大寫 void ignore
1 package demo03; 2 3 //String類的構造方法 4 public class StringDemo { 5 public static void main(String[] args) { 6 7 String str1 = "str1"; //定義方式1 8 String str2 = new String("str1"); //定義方式2 9 10 System.out.println(str1);11 System.out.println(str2);12 13 //false str1==str2 比較的是記憶體位址14 System.out.println(str1==str2); 15 //true String類 重寫equals() 比較的是每個字元16 System.out.println(str1.equals(str2));17 18 byte[] by = {97,98,99,100};19 String str3 = new String(by); //構造方法 String(byte[] bytes) 傳位元組數組20 System.out.println(str3); //abcd21 22 String str4 = new String(by,1,3); //構造方法 String(byte[] bytes int offset, int length)23 System.out.println(str4); //bcd24 25 char[] charArray = {‘i‘,‘j‘,‘k‘};26 String str5 = new String(charArray); //構造方法 String(char[] value) 傳字元數組27 System.out.println(str5); //ijk28 29 String str6 = new String(charArray,1,2);//構造方法 String(char[] value, int offset, int count)30 System.out.println(str6); //jk31 32 33 34 35 }36 }
1 package demo03; 2 3 public class StringDemo1 { 4 public static void main(String[] args) { 5 String s1 = "abcdefgabcdefg"; 6 7 int length = s1.length(); //獲得字串長度 8 System.out.println(length); //14 9 10 String s2 = s1.substring(0,3); //截取字串 兩個參數11 String s3 = s1.substring(7); //一個參數12 System.out.println(s2); //abc13 System.out.println(s3); //abcdefg14 15 boolean b1 = s1.startsWith("abc"); //是否以指定字串開始16 System.out.println(b1); //true17 18 boolean b2 = s1.endsWith("efg"); //是否以指定字串結尾19 System.out.println(b2); //true20 21 boolean b3 = s1.contains("gab"); //是否包含指定字串22 System.out.println(b3); //true23 24 int index1 = s1.indexOf(‘c‘); //返回指定字元第一次出現的索引25 int index2 = s1.indexOf(‘x‘);26 System.out.println(index1); //227 System.out.println(index2); //-128 29 byte[] bytes = s1.getBytes(); //字串轉為位元組數組30 for (int i = 0; i < bytes.length; i++) {31 System.out.println(bytes[i]);32 }33 34 char[] ch = s1.toCharArray(); //字串轉為字元數組35 for (int i = 0; i < ch.length; i++) {36 System.out.println(ch[i]);37 }38 39 String s5 = "Abc";40 String s6 = "abc";41 boolean b5 = s5.equals(s6); //比較字串42 boolean b6 = s5.equalsIgnoreCase(s6); //比較字串 忽略大小寫43 System.out.println(b5); //false44 System.out.println(b6); //true45 46 47 48 }49 }
demo
1 package demo03; 2 3 public class StringLianXi { 4 public static void main(String[] args) { 5 getCount("fhdkfADfDf123F"); 6 System.out.println(toConvert("aVdfDfd")); 7 System.out.println(getStringCount("helloabc,abc,abcabcfjdklsfjd", "abc")); 8 } 9 10 //擷取指定字串中 大寫字母, 小寫字母, 數字 的個數11 public static void getCount(String s) {12 int upper = 0;13 int lower = 0;14 int digit = 0;15 16 for (int i = 0; i < s.length(); i++) {17 char c = s.charAt(i);18 19 /*if (c>=65 && c<=90) {20 upper++;21 } else if (c>=97 && c<=122) {22 lower++;23 } else if (c>=48 && c<=57) {24 digit++;25 }*/26 27 if (c>=‘A‘ && c<=‘Z‘) {28 upper++;29 } else if (c>=‘a‘ && c<=‘z‘) {30 lower++;31 } else if (c>=‘0‘ && c<=‘9‘) {32 digit++;33 }34 }35 36 System.out.println(upper);37 System.out.println(lower);38 System.out.println(digit);39 }40 41 //將指定字串第一個字母大寫, 其它字母小寫42 public static String toConvert(String s) {43 String first = s.substring(0,1);44 String after = s.substring(1);45 first = first.toUpperCase();46 after = after.toLowerCase();47 48 return first+after;49 }50 51 //查詢指定字串中出現其它字串的次數52 public static int getStringCount(String s, String key) {53 int count = 0;54 int index = 0;55 56 while ((index = s.indexOf(key)) != -1) {57 count++;58 s = s.substring(index+key.length());59 }60 61 return count;62 }63 64 }
Java String類