字串-String

是一種參考資料型態,當宣告 String str = "abc";  時,會在stack記憶體中開一個空間名為str,然後在heap記憶體的【字串池】中開一個放置字串"abc"

由字元所組成的一串文字符號被稱之為「字串」,例如 "Hello" 這個字串就是由 'H'、'e'、'l'、'l'、'o' 這五個字元 所組成,在某些程式語言中,字串是以字元陣列的方式存在

 

以下面的問題作範例,繪製記憶體的狀態

 

== 與 equals的差別

==在參考資料型態中,是比較兩個資料存放的"地址"是否相同

equals則是比要兩者存放的資料"值"是否相同

所以不是說字串要比較相等就一定要用equals,而是要看到底是要比較什麼

 

length 的說明

String 有一個方法 length(),可以獲取字串的長度

陣列Array中,有一個屬性,可以獲取陣列長度,length

為什麼特別在陣列中,才有length屬性呢?

陣列他在創建的時候,就必須設定好它的長度了,且不能再被改變

而String背後的組成,其實是由char[ ],再背後是由byte[ ] 組成的,因此已經有length屬性,就不需要在String類別中再設置一次length屬性

https://www.it145.com/9/24708.html

 

 

String轉成char、byte陣列 

String 背後其實是由char陣列組成的,char陣列又是由byte陣列組成的,所以我們可以透過以下方法將String轉成char陣列及byte陣列

public byte[] getBytes(Charset charset)
    


public char[] toCharArray()

 

用一個題目來練習:鍵盤輸入字串, 統計有多少 大寫、小寫、數字、其他

第一種是轉成byte陣列,再查ascii表,但其實char本身就可以自動轉換成int了

package tw.lillian.code;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/*
鍵盤輸入字串
統計有多少 大寫、小寫、數字、其他

*/
public class MainClass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int upCase = 0;
        int smallCase = 0;
        int num = 0;


        System.out.println("Enter a String");
        String input = sc.nextLine();

        for (int i = 0; i < input.length(); i++) {
            byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
            if (bytes[i] >= 65 && bytes[i] <= 90) {
                upCase++;
            } else if (bytes[i] >= 97 && bytes[i] <= 122) {
                smallCase++;
            } else if (bytes[i] >= 48 && bytes[i] <= 57) {
                num++;
            }
        }

        System.out.println("大寫:" + upCase + "\n小寫:" + smallCase + "\n數字:" + num);

    }
}

 

第二種用char陣列

package tw.lillian.code;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/*
鍵盤輸入字串
統計有多少 大寫、小寫、數字、其他

*/
public class MainClass02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int upCase = 0;
        int lowerCase = 0;
        int num = 0;
        int other = 0;


        System.out.println("Enter a String");
        String input = sc.nextLine();

        for (int i = 0; i < input.length(); i++) {
            char[] bytes = input.toCharArray();
            if (bytes[i] >= 'A' && bytes[i] <= 'Z') {
                upCase++;
            } else if (bytes[i] >= 'a' && bytes[i] <= 'z') {
                lowerCase++;
            } else if (bytes[i] >= '0' && bytes[i] <= '9') {
                num++;
            }else{
                other++;
            }
        }

        System.out.println("大寫:" + upCase + "\n小寫:" + lowerCase + "\n數字:" + num + "\n其他:" + other);

    }
}

 

最後一種不用轉換,直接charAt()就可以

package tw.lillian.code;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/*
鍵盤輸入字串
統計有多少 大寫、小寫、數字、其他

*/
public class MainClass02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int upCase = 0;
        int lowerCase = 0;
        int num = 0;
        int other = 0;


        System.out.println("Enter a String");
        String input = sc.nextLine();

        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i) >= 'A' && input.charAt(i) <= 'Z') {
                upCase++;
            } else if (input.charAt(i) >= 'a' && input.charAt(i) <= 'z') {
                lowerCase++;
            } else if (input.charAt(i) >= '0' && input.charAt(i) <= '9') {
                num++;
            }else{
                other++;
            }
        }

        System.out.println("大寫:" + upCase + "\n小寫:" + lowerCase + "\n數字:" + num + "\n其他:" + other);

    }
}

 

文章標籤
全站熱搜
創作者介紹
創作者 Lillian 的頭像
Lillian

安安的code日記

Lillian 發表在 痞客邦 留言(0) 人氣(2)