Java String類
阿新 • • 發佈:2017-05-20
count 包含 第一個字母大寫 獲取 rgs ret lower charat index
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); //abcd 21 22 String str4 = new String(by,1,3); //構造方法 String(byte[] bytes int offset, int length) 23 System.out.println(str4); //bcd 24 25 char[] charArray = {‘i‘,‘j‘,‘k‘}; 26 String str5 = newString(charArray); //構造方法 String(char[] value) 傳字符數組 27 System.out.println(str5); //ijk 28 29 String str6 = new String(charArray,1,2);//構造方法 String(char[] value, int offset, int count) 30 System.out.println(str6); //jk 31 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); //abc 13 System.out.println(s3); //abcdefg 14 15 boolean b1 = s1.startsWith("abc"); //是否以指定字符串開始 16 System.out.println(b1); //true 17 18 boolean b2 = s1.endsWith("efg"); //是否以指定字符串結尾 19 System.out.println(b2); //true 20 21 boolean b3 = s1.contains("gab"); //是否包含指定字符串 22 System.out.println(b3); //true 23 24 int index1 = s1.indexOf(‘c‘); //返回指定字符第一次出現的索引 25 int index2 = s1.indexOf(‘x‘); 26 System.out.println(index1); //2 27 System.out.println(index2); //-1 28 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); //false 44 System.out.println(b6); //true 45 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類