Sting练习
package com.book.lite;import java.util.Locale;
import java.util.Scanner;/*1. 字符串的兩種定義方式的區別:String a1 = ""; 只有一個對象:String a2 = new String(); 有兩個對象2. String類的特點是什么底層代碼的不變性,將字符串拆分成字符數組char[]3. 編寫程序證明String類的不變性4. 編程題A. 自定義方法,實現兩個字符串的比較,完全相等返回trueB. 將字符串中的字符進行大小寫互轉C. 判斷一個字符串是否是回文。例如 abcba aba要求字符串長度必須是奇數,不能少于3個字符5. String類和StringBuffer的區別6. 自定義方法,實現字符串的反轉*/
public class DayHomeWorkOf13 {public static void main(String[] args) {
//System.out.println(getString_6());}public static void getString_3() {String b1 = "hello ";String b2 = b1.toUpperCase(Locale.ROOT);System.out.println(b1 + "我沒變");System.out.println(b2 + "我變大寫");}public static boolean getString_4() {Scanner sc = new Scanner(System.in);System.out.print("請輸入第一個字符串:");String sc1 = sc.nextLine();sc = new Scanner(System.in);System.out.print("請輸入第二個字符串:");String sc2 = sc.nextLine();return sc1.equals(sc2);}//B. 將字符串中的字符進行大小寫互轉public static String getString_5() {Scanner sc = new Scanner(System.in);System.out.print("請輸入要轉換的字符串:");String sc1 = sc.nextLine();//判斷字符串是否為空if (sc1.length()==0){return "請輸入字符串";}else {//將字符串轉為字符數組char [] ch = sc1.toCharArray();//遍歷數組for(int x=0 ;x < ch.length; x++){//判斷字符串的大小寫[a-z 97-122] [A-Z 65-90]if(ch[x]>=97 && ch[x] <=122 ){ch[x] -= 32;}else if(ch[x]>=65 && ch[x]<=90){ch[x] += 32;}elsereturn "請輸入正確數值";}return new String(ch);}}//C. 判斷一個字符串是否是回文。例如 abcba abapublic static boolean getString_6(){Scanner sc = new Scanner(System.in);System.out.print("請輸入要判斷的字符串:");String sc1 = sc.nextLine();if (sc1.length()==0 )return false;else if (sc1.length() % 2 ==0 )return false;else {//將字符串轉換為數組char[] ch = sc1.toCharArray();//獲取數組中最遠數組值for (int min = 0 , max = ch.length-1;min < max;max--,min++){if (ch[min]!=ch[max])return false;}return true;}}
}
總結
- 上一篇: 第4章 Python 数字图像处理(DI
- 下一篇: Vivado 2019使用教程