日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

字符串是什么意思 字符怎么輸入( 四 )


9.7 操縱字符串
獲取字符 charAt(int index)獲取指定位置的字符String sentence = "蓋倫,在進(jìn)行了連續(xù)8次擊殺后,獲得了 超神 的稱(chēng)號(hào)"; char c = sentence.charAt(0); 復(fù)制代碼
獲取對(duì)應(yīng)的字符數(shù)組 toCharArray() String sentence = "蓋倫,在進(jìn)行了連續(xù)8次擊殺后,獲得了超神 的稱(chēng)號(hào)"; char[] cs = sentence.toCharArray(); //獲取對(duì)應(yīng)的字符數(shù)組
截取子字符串 subStringString sentence = "蓋倫,在進(jìn)行了連續(xù)8次擊殺后,獲得了 超神 的稱(chēng)號(hào)";//截取從第3個(gè)開(kāi)始的字符串 (基0)//到5-1的位置的字符串//左閉右開(kāi)String subString2 = sentence.substring(3,5);System.out.println(subString2);
split 根據(jù)分隔符進(jìn)行分隔String sentence = "蓋倫,在進(jìn)行了連續(xù)8次擊殺后,獲得了 超神 的稱(chēng)號(hào)"; //根據(jù),進(jìn)行分割,得到3個(gè)子字符串String subSentences[] = sentence.split(",");for (String sub : subSentences) {System.out.println(sub);}
trim 去掉首位空格String sentence = "蓋倫,在進(jìn)行了連續(xù)8次擊殺后,獲得了 超神 的稱(chēng)號(hào)";System.out.println(sentence);//去掉首尾空格System.out.println(sentence.trim());
大小寫(xiě)toLowerCase 全部變成小寫(xiě);toUpperCase 全部變成大寫(xiě) 。
String sentence = "Garen";//全部變成小寫(xiě)System.out.println(sentence.toLowerCase());//全部變成大寫(xiě)System.out.println(sentence.toUpperCase());
定位indexOf 判斷字符或者子字符串出現(xiàn)的位置 contains 是否包含子字符串
String sentence = "蓋倫,在進(jìn)行了連續(xù)8次擊殺后,獲得了超神 的稱(chēng)號(hào)";System.out.println(sentence.indexOf('8')); //字符第一次出現(xiàn)的位置System.out.println(sentence.indexOf("超神")); //字符串第一次出現(xiàn)的位置System.out.println(sentence.lastIndexOf("了")); //字符串最后出現(xiàn)的位置System.out.println(sentence.indexOf(',',5)); //從位置5開(kāi)始 , 出現(xiàn)的第一次,的位置System.out.println(sentence.contains("擊殺")); //是否包含字符串"擊殺"
替換replaceAll 替換所有的;replaceFirst 只替換第一個(gè) 。
String sentence = "蓋倫,在進(jìn)行了連續(xù)8次擊殺后,獲得了超神 的稱(chēng)號(hào)"; String temp = sentence.replaceAll("擊殺", "被擊殺"); //替換所有的temp = temp.replaceAll("超神", "超鬼");System.out.println(temp);temp = sentence.replaceFirst(",","");//只替換第一個(gè)System.out.println(temp);
練習(xí)1 - 每個(gè)單詞的首字母都轉(zhuǎn)換為大寫(xiě)給出一句英文句子: "let there be light" 得到一個(gè)新的字符串,每個(gè)單詞的首字母都轉(zhuǎn)換為大寫(xiě)
練習(xí)2 - 英文繞口令英文繞口令 peter piper picked a peck of pickled peppers 統(tǒng)計(jì)這段繞口令有多少個(gè)以p開(kāi)頭的單詞
練習(xí)3 - 間隔大寫(xiě)小寫(xiě)模式把 lengendary 改成間隔大寫(xiě)小寫(xiě)模式,即 LeNgEnDaRy
練習(xí)4 - 最后一個(gè)字母變大寫(xiě)把 lengendary 最后一個(gè)字母變大寫(xiě)
練習(xí)5 - 把最后一個(gè)two單詞首字母大寫(xiě)Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak 把最后一個(gè)two單詞首字母大寫(xiě)
public class StringTest {//問(wèn)題1 - 每個(gè)單詞的首字母都轉(zhuǎn)換為大寫(xiě)public static String[] Split_Space(String str){return str.split(" ");}public static String[] Toupcase(String[] strs){String []newstrs = new String[strs.length];int i = 0;for(String str : strs){char []cs = str.toCharArray();cs[0] = Character.toUpperCase(cs[0]);newstrs[i++] = String.valueOf(cs);}return newstrs;}//練習(xí)2 - 英文繞口令public static int countp(String[] strs){int count = 0;for (String str :strs){if (str.charAt(0) == 'p')count++;}return count;}//練習(xí)3 - 間隔大寫(xiě)小寫(xiě)模式public static String updownChange(String str){String upstr = str.toUpperCase();char []cs = upstr.toCharArray();for (int i = 1;i < cs.length;i+=2) {cs[i] = Character.toLowerCase(cs[i]);}return new String(cs);}//練習(xí)4-最后一個(gè)字母變大寫(xiě)public static String lastup(String str){char []cs = str.toCharArray();cs[cs.length - 1] = Character.toUpperCase(cs[cs.length - 1]);return new String(cs);}//練習(xí)5 - 把最后一個(gè)two單詞首字母大寫(xiě)public static String last2up(String s){char [] c = s.toCharArray();String n = "two";c [s.lastIndexOf(n)] = Character.toUpperCase(s.charAt(s.lastIndexOf(n)));return new String(c);}public static void main(String[] args) {//問(wèn)題1 - 每個(gè)單詞的首字母都轉(zhuǎn)換為大寫(xiě)String str = "let there be light";String []newstrs = Toupcase(Split_Space(str));String result = "";for (String s : newstrs) {result += s + " ";}System.out.println(result);//練習(xí)2 - 英文繞口令String str2 = "peter piper picked a peck of pickled peppers";int count = countp(Split_Space(str2));System.out.println("首字母為p的單詞有" + count);//練習(xí)3 - 間隔大寫(xiě)小寫(xiě)模式String str3 = "lengendary";System.out.println("間隔大小寫(xiě)模式:" + updownChange(str3));//練習(xí)4-最后一個(gè)字母變大寫(xiě)System.out.println("最后一個(gè)字符變成大寫(xiě):" + lastup(str3));//練習(xí)5 - 把最后一個(gè)two單詞首字母大寫(xiě)String str5 = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";System.out.println(last2up(str5));}}

推薦閱讀