String創建的倆種方式
1.直接賦值
String str = "Hello World!";
當使用直接賦值的方式去創建字串的時候,虛擬機會去串池里去檢查字串是否存在,如果沒有會創建一個然后將地址值賦值給變數;有的話就直接將地址賦值給變數,可以復用,推薦這樣寫節省記憶體,記住!!!存盤在串池!!!
2.使用new去創建
//常用的構造,其他構造方法可以自己去api檔案查看
String str = new String();
String str = new String(char[] arr);//將字符型別陣列轉換為字串
String str = new String(byte[] arr);//將位元組型別陣列轉換為字串
使用new去創建字串,虛擬機會在堆里開辟一個新的空間,然后將地址值賦值給變數,不推薦空參創建,
字串的比較
小tips: ==號比的是什么?
分為倆種情況一種是基本資料型別,一種是參考型別
基本資料型別:具體比的是值
參考資料型別: 比的是地址值
進入真題
字串我們已經知道是參考資料型別,那我們就不能使用==去比較他們的值,除了直接賦值去可以比較他們的地址值,因為串池里的字串是可以復用的,
字串的比較主要有倆種方式:
boolean equals(); //區分大小寫
boolean equalsIgnoreCase() //不區分大小寫
常用方法
常用方法主要有倆種,一種是截取,一種是替換:
Sting substring(開始索引, 結束索引); //截取字串,包頭不包尾,取不到結束索引
String replace(舊值,要替換的值); //舊值都替換成要替換的值
StringBuilder跟StringJoiner
它們都可以看成一個容器,創建之后里面的內容是可變的,
StringBuilder主要作用是提高字串的操作效率
StingJoiner主要作用是提升拼接字串的效率
StringBuilder的常用方法
public StringBuilder append(任意型別); //添加資料回傳物件本身
public StringBuilder reverse() //反轉容器中的內容
public int length() //回傳長度(字符出現的個數)
public String toString() //實作在StringBuilder轉成String
StringJoiner的構造方法
new StringJoiner("間隔符號");
new StringJoiner(間隔符號,開始符號,結束符號);
StringJoiner的常用方法
add(); //添加元素到容器
length(); //回傳字符的個數
toString(); //轉成字串
附上例題
1. 鍵盤錄入一個字串 要求長度小于9 只能是數字轉換為阿拉伯數字
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//1. 鍵盤錄入一個字串 要求長度小于9 只能是數字
System.out.print("請輸入符合規則的字串: ");
String str = "";
while (true) {
str = sc.next();
boolean b = checkStr(str);
if (b) {
break;
} else {
System.out.print("您輸入的字串不符合規則,請重新輸入: ");
}
}
//2. 將字串轉換為羅馬數字
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
int index = str.charAt(i) - 48;
sb.append(changeLuoMa(index));
}
//4. 輸出結果
System.out.println(sb.toString());
}
// 將字符轉換為羅馬數字
public static String changeLuoMa(int index) {
String[] str = {" ", "I ", "II ", "III ", "IV ", "VI ", "VII ", "VIII ", "IX "};
return str[index];
}
//字串檢查
public static boolean checkStr(String str) {
if (str.length() > 9) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
return false;
}
}
return true;
}
2. 旋轉比較倆個字串是否會相等
public static void main(String[] args) {
String str1 = "abcd";
String str2 = "bcda";
boolean flag = check(str1, str2);
System.out.println(flag);
}
private static boolean check(String str1, String str2) {
for (int i = 0; i < str1.length(); i++) {
str1 = rotate(str1);
if(str1.equals(str2)) {
return true;
}
}
return false;
}
public static String rotate(String str) {
char first = str.charAt(0);
String end = str.substring(1);
return end + first;
}
3. 鍵盤輸入任意字串,打亂里面的內容
public static void main(String[] args) {
//鍵盤輸入任意字串,打亂里面的內容
Scanner sc = new Scanner(System.in);
Random random = new Random();
System.out.print("請輸入字串: ");
String str = sc.next();
char[] arr = str.toCharArray();
for (int i = 0; i < arr.length; i++) {
int index = random.nextInt(arr.length);
char temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
System.out.println(new String(arr));
}
4. 生成驗證碼
//生成驗證碼
//內容:可以是小寫字母,也可以是大寫字母,還可以是數字
//規則:
//長度為5
//內容中是四位字母,1位數字,
//其中數字只有1位,但是可以出現在任意的位置,
public static void main(String[] args) {
Random random = new Random();
char[] arr = getCharArr();
char[] numArr = {'0','1','2','3','4','5','6','7','8','9'};
char[] result = new char[5];
for (int i = 0; i < result.length - 1; i++) {
int index = random.nextInt(arr.length);
result[i] = arr[index];
}
result[result.length - 1] = numArr[random.nextInt(numArr.length)];
for (int i = 0; i < result.length; i++) {
int index = random.nextInt(result.length);
char temp = result[i];
result[i] = result[index];
result[index] = temp;
}
System.out.println(result);
}
//創建隨機的字母陣列
public static char[] getCharArr() {
char[] arr = new char[48];
for (int i = 0; i < arr.length; i++) {
if (i < 24) {
arr[i] = (char) (65 + i);
} else {
arr[i] = (char) (97 + i - 24);
}
}
return arr;
}
5. 由鍵盤錄入一個字串,統計字串中英文字母和數字分別有多少個
//請撰寫程式,由鍵盤錄入一個字串,統計字串中英文字母和數字分別有多少個,比如:Hello12345World中字母:10個,數字:5個,
public static void main(String[] args) {
int numberCount = 0;
int charCount = 0;
Scanner sc = new Scanner(System.in);
System.out.print("請輸入字串: ");
String str = sc.next();
char[] arr = str.toCharArray();
for (int i = 0; i < arr.length; i++) {
if(arr[i] >= '0' && arr[i] <= '9') {
numberCount++;
}else if((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] > 'A' && arr[i] < 'Z')) {
charCount++;
}
}
System.out.println("numberCount => " + numberCount);
System.out.println("charCount => " + charCount);
}
6. 判斷一個字串是否是對稱的字串
//請定義一個方法用于判斷一個字串是否是對稱的字串,并在主方法中測驗方法,例如:"abcba"、"上海自來水來自海上"均為對稱字串,
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入字串: ");
String str = sc.next();
StringBuilder sb = new StringBuilder(str);
String s = sb.reverse().toString();
boolean result = str.equals(s);
System.out.println("result => " + result);
}
7. 檢驗身份證
//我國的居民身份證號碼,由由十七位數字本體碼和一位數字校驗碼組成,請定義方法判斷用戶輸入的身份證號碼是否合法,
// 并在主方法中呼叫方法測驗結果,規則為:號碼為18位,不能以數字0開頭,前17位只可以是數字,最后一位可以是數字或者大寫字母X,
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入您的身份證: ");
String id = sc.next();
boolean result = isOk(id);
System.out.println("result => " + result);
}
public static boolean isOk(String str) {
if (str.length() != 18) {
return false;
}
if (str.charAt(0) == '0') {
return false;
}
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
return false;
}
}
char end = str.charAt(str.length() - 1);
if (end == 'X' || end == 'x' || (end < '9' && end > '0')) {
}else {
return false;
}
return true;
}
8. int陣列轉換為字串
//定義一個方法,把 int 陣列中的資料按照指定的格式拼接成一個字串回傳,呼叫該方法,并在控制臺輸出結果,
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
String result = print(arr);
System.out.println("result => " + result);
}
public static String print(int[] arr) {
StringJoiner sj = new StringJoiner(", ", "[", "]");
for (int i = 0; i < arr.length; i++) {
sj.add(arr[i] + "");
}
return sj.toString();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/533505.html
標籤:其他
上一篇:06python序列
