<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 字符串 > 原文: [https://www.programiz.com/java-programming/string](https://www.programiz.com/java-programming/string) #### 在本教程中,我們將在示例的幫助下了解 Java `String`,如何創建 Java `String`及其各種方法。 在 Java 中,字符串是字符序列。 例如,`"hello"`是包含以下字符序列的字符串:`'h'`,`'e'`,`'l'`,`'l'`和`'o'`。 與其他編程語言不同,Java 中的字符串不是原始類型(例如`int`,`char`等)。 相反,所有字符串都是名為`String`的預定義類的對象。 例如, ```java // create a string String type = "java programming"; ``` 在這里,我們創建了一個名為`type`的字符串。 在這里,我們使用`"java programming`初始化了字符串。 在 Java 中,我們使用**雙引號**表示字符串。 該字符串是`String`類的實例。 **注意**:所有字符串變量都是`String`類的實例。 * * * ## Java 字符串方法 Java `String`提供了各種方法,使我們可以執行不同的字符串操作。 以下是一些常用的字符串方法。 | 方法 | 描述 | | --- | --- | | `concat()` | 將兩個字符串連接在一起 | | `equals()` | 比較兩個字符串的值 | | `charAt()` | 返回存在于指定位置的字符 | | `getBytes()` | 將字符串轉換為字節數組 | | `indexOf()` | 返回字符串中指定字符的位置 | | `length()` | 返回指定字符串的大小 | | `replace()` | 將指定的舊字符替換為指定的新字符 | | `substring()` | 返回字符串的子字符串 | | `split()` | 將字符串分成字符串數組 | | `toLowerCase()` | 將字符串轉換為小寫 | | `toUpperCase()` | 將字符串轉換為大寫 | | `valueOf()` | 返回指定數據的字符串表示形式 | 讓我們舉幾個例子。 * * * ### 示例 1:查找字符串的長度 ```java class Main { public static void main(String[] args) { // create a string String greet = "Hello! World"; System.out.println("The string is: " + greet); //checks the string length System.out.println("The length of the string: " + greet.length()); } } ``` **輸出** ```java The string is: Hello! World The length of the string: 12 ``` 在上面的示例中,我們創建了一個名為`greet`的字符串。 這里我們使用了`length()`方法來獲取字符串的大小。 * * * ### 示例 2:使用`concat()`連接兩個字符串 ```java class Main { public static void main(String[] args) { // create string String greet = "Hello! "; System.out.println("First String: " + greet); String name = "World"; System.out.println("Second String: " + name); // join two strings String joinedString = greet.concat(name); System.out.println("Joined String: " + joinedString); } } ``` **輸出**: ```java First String: Hello! Second String: World Joined String: Hello! World ``` 在上面的示例中,我們創建了 2 個名為`greet`和`name`的字符串。 在這里,我們使用了`concat()`方法來連接字符串。 因此,我們得到一個名為`joinedString`的新字符串。 * * * 在 Java 中,我們還可以使用`+`運算符連接兩個字符串。 ### 示例 3:使用`+`運算符連接字符串 ```java class Main { public static void main(String[] args) { // create string String greet = "Hello! "; System.out.println("First String: " + greet); String name = "World"; System.out.println("Second String: " + name); // join two strings String joinedString = greet + name; System.out.println("Joined String: " + joinedString); } } ``` **輸出**: ```java First String: Hello! Second String: World Joined String: Hello! World ``` 在這里,我們使用了`+`運算符來連接兩個字符串。 * * * ### 示例 4:比較兩個字符串 ```java class Main { public static void main(String[] args) { // create strings String first = "java programming"; String second = "java programming"; String third = "python programming"; // compare first and second strings boolean result1 = first.equals(second); System.out.println("Strings first and second are equal: " + result1); //compare first and third strings boolean result2 = first.equals(third); System.out.println("Strings first and third are equal: " + result2); } } ``` **輸出**: ```java Strings first and second are equal: true Strings first and third are equal: false ``` 在上面的示例中,我們使用`equals()`方法比較兩個字符串的值。 如果兩個字符串相同,則該方法返回`true`,否則返回`false`。 **注意**:我們也可以使用`==`運算符和`compareTo()`方法在 2 個字符串之間進行比較。 * * * ### 示例 5:從字符串中獲取字符 ```java class Main { public static void main(String[] args) { // create string using the string literal String greet = "Hello! World"; System.out.println("The string is: " + greet); // returns the character at 3 System.out.println("The character at 3: " + greet.charAt(3)); // returns the character at 7 System.out.println("The character at 7: " + greet.charAt(7)); } } ``` **輸出**: ```java The string is: Hello! World The character at 3: l The character at 7: W ``` 在上面的示例中,我們使用`charAt()`方法從指定位置訪問字符。 * * * ### 示例 6:`String`其他方法 ```java class Main { public static void main(String[] args) { // create string using the new keyword String example = new String("Hello! World"); // returns the substring World System.out.println("Using the subString(): " + example.substring(7)); // converts the string to lowercase System.out.println("Using the toLowerCase(): " + example.toLowerCase()); // converts the string to uppercase System.out.println("Using the toUpperCase(): " + example.toUpperCase()); // replaces the character '!' with 'o' System.out.println("Using the replace(): " + example.replace('!', 'o')); } } ``` **輸出**: ```java Using the subString(): World Using the toLowerCase(): hello! world Using the toUpperCase(): HELLO! WORLD Using the replace(): Helloo World ``` 在上面的示例中,我們使用`new`關鍵字創建了一個名為`example`的字符串。 這里, * `substring()`方法返回字符串`World` * `toLowerCase()`方法將字符串轉換為小寫 * `toUpperCase()`方法將字符串轉換為大寫 * `replace()`方法替換字符`'!'`與`'o'`。 * * * ## 字符串中的轉義字符 Java 中的字符串由**雙引號**表示。 例如, ```java // create a string String example = "This is a string"; ``` 現在,如果我們想在字符串中包含**雙引號**。 例如, ```java // include double quote String example = "This is the "String" class"; ``` 這將導致錯誤。 這是因為**雙引號**用于表示字符串。 因此,編譯器會將`"this"`視為字符串。 為了解決此問題,在 Java 中使用了轉義字符`\`。 現在我們可以在字符串中包含**雙引號**,如下所示: ```java // use the escape character String example = "This is the \"String\" class."; ``` 轉義字符告訴編譯器轉義**雙引號**并讀取全文。 * * * ## Java 字符串是不可變的 在 Java 中,創建字符串意味著創建`String`類的對象。 創建字符串時,無法在 Java 中更改該字符串。 這就是為什么在 Java 中將字符串稱為**不可變**的原因。 為了更深入地理解它,讓我們考慮一個示例: ```java // create a string String example = "Hello!"; ``` 在這里,我們創建了一個字符串對象`"Hello!"`。 創建之后,我們將無法更改它。 現在假設我們要更改字符串。 ```java // adds another string to the string example = example.concat(" World"); ``` 在這里,我們嘗試將新的字符串添加到先前的字符串。 由于字符串是**不變的**,因此應引起錯誤。 但這很好。 現在看來我們可以更改字符串了。 但是,事實并非如此。 讓我們看看這里實際發生了什么。 我們有一個字符串`"Hello!"`,由名為`example`的變量引用。 現在,在執行上面的代碼時, * JVM 使用字符串`"Hello!"` * 附加字符串`"World"` * 這將創建一個新字符串`"Hello!World"` * 變量`example`現在引用新字符串 * 上一個字符串`"Hello!"`保持不變 **注意**:每次創建新字符串并由變量引用它時。 * * * ## 使用`new`關鍵字創建字符串 到目前為止,我們已經在 Java 中創建了類似于基本類型的字符串。 但是,由于 Java 中的字符串是對象,因此我們也可以使用`new`關鍵字進行創建。 例如, ```java // create a string using the new keyword String name = new String("java string"); ``` 在上面的示例中,我們將`new`關鍵字與構造器`String()`一起使用來創建字符串。 `String`類提供了各種其他構造器來創建字符串。 要了解所有這些構造器,請訪問 [Java `String`(Java 官方文檔)](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html "Official Oracle String Documentation")。 現在,讓我們看一下創建字符串的過程與上一個過程有何不同。 * * * ## 使用字符串字面值和`new`關鍵字之間的區別 現在我們知道如何使用字符串字面值和`new`關鍵字創建字符串,讓我們看看它們之間的主要區別是什么。 在 Java 中,JVM 維護一個字符串池以將其所有字符串存儲在內存中。 字符串池有助于重用字符串。 使用字符串字面值創建字符串時,將直接提供字符串的值。 因此,編譯器首先檢查字符串池以查看字符串是否已經存在。 * **如果字符串已經存在**,則不會創建新字符串。 而是,新引用指向現有的字符串。 * **如果字符串不存在**,則創建新字符串。 但是,在使用`new`關鍵字創建字符串時,不會直接提供字符串的值。 因此,新字符串始終被創建。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看