<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 字符串類指南 > 原文: [https://howtodoinjava.com/java-string/](https://howtodoinjava.com/java-string/) **Java 字符串**表示[不可變的](https://howtodoinjava.com/java/string/java-interview-question-why-strings-are-immutable/)字符序列,并且一旦創建就無法更改。 字符串的類型為`java.lang.String`類。 在此頁面中,學習有關使用字符串字面值和[構造器](https://howtodoinjava.com/oops/java-constructors/),字符串方法以及與字符串轉換和格式設置有關的各種字符串示例創建字符串的信息。 ## 1\. 用 Java 創建字符串 用 Java 創建字符串的方法有兩種。 1. #### 字符串字面量 字符串字面值最簡單,建議使用**在 Java 中創建字符串**。 這樣,只需將雙引號中的字符分配給`java.lang.String`類型的變量。 ```java String blogName = "howtodoinjava.com"; String welcomeMessage = "Hello World !!"; ``` 字符串字面值存儲在字符串池中,字符串池是由 JVM 創建的特殊內存區域。 一個`String`只能有一個實例。 具有相同字符序列的任何第二個`String`都將第一個字符串的引用存儲在字符串池中。 它可以高效地使用字符串,并在運行時節省大量物理內存。 ```java String blogName1 = "howtodoinjava.com"; String blogName2 = "howtodoinjava.com"; String blogName3 = "howtodoinjava.com"; String blogName4 = "howtodoinjava.com"; String blogName5 = "howtodoinjava.com"; ``` 在上面的示例中,我們創建了 5 個具有相同`char`序列的字符串字面值。 在 JVM 內部,字符串池中只有一個`String`實例。 所有其余的 4 個實例將共享為第一個字面值創建的字符串字面值的引用。 2. #### 字符串對象 有時,我們可能希望為內存中的每個單獨的字符串創建單獨的實例。 我們可以使用**新的**關鍵字為每個字符串值創建一個字符串對象。 使用`new`關鍵字創建的字符串對象 – 存儲在*堆內存*中。 ```java String blogName1 = new String("howtodoinjava.com"); String blogName2 = new String("howtodoinjava.com"); String blogName3 = new String("howtodoinjava.com"); ``` 在上面的示例中,堆內存中將有 3 個具有相同值的`String`實例。 ## 2\. Java 字符串方法 1. [`char charAt(int index)`](https://howtodoinjava.com/java/string/string-charat-method-example/) – 返回指定索引處的字符。 指定的索引值應介于`0`至`length() -1`之間(包括兩個端點)。 如果索引無效/超出范圍,則拋出`IndexOutOfBoundsException`。 ```java String blogName = "howtodoinjava.com"; char c = blogName.charAt(5); //'d' ``` 2. [`boolean equals(Object obj)`](https://howtodoinjava.com/java/string/string-equals-method/) – 將字符串與指定的字符串進行比較,如果兩者均匹配,則返回`true`,否則返回`false`。 ```java String blogName = "howtodoinjava.com"; blogName.equals( "howtodoinjava.com" ); //true blogName.equals( "example.com" ); //false ``` 3. [`boolean equalsIgnoreCase(String str)`](https://howtodoinjava.com/java/string/string-equalsignorecase-method/) – 與`equals`方法相同,但不區分大小寫。 ```java String blogName = "howtodoinjava.com"; blogName.equalsIgnoreCase( "howtodoinjava.com" ); //true blogName.equalsIgnoreCase( "HowToDoInJava.com" ); //true ``` 4. [`int compareTo(String string)`](https://howtodoinjava.com/java/string/java-string-compareto-method/) – 根據字符串中每個字符的 Unicode 值按字典順序比較兩個字符串。 您可以考慮基于字典的比較。 如果參數字符串等于此字符串,則返回值為 0;否則,返回值為 0。 如果此字符串在字典上小于字符串參數,則小于 0 的值; 如果該字符串在字典上大于字符串參數,則該值大于 0。 ```java String blogName = "howtodoinjava.com"; blogName.compareTo( "HowToDoInJava.com" ); //32 blogName.compareTo( "example.com" ); //3 ``` 5. [`int compareToIgnoreCase(String str)`](https://howtodoinjava.com/java/string/string-comparetoignorecase-example/) – 與`CompareTo`方法相同,但是在比較期間忽略大小寫。 ```java String blogName = "howtodoinjava.com"; blogName.compareToIgnoreCase( "HowToDoInJava.com" ); //0 blogName.compareToIgnoreCase( "example.com" ); //3 ``` 6. [`boolean startsWith(String prefix, int offset)`](https://howtodoinjava.com/java/string/java-string-startswith-example/) – 從指定的偏移量索引開始,檢查`String`是否具有指定的前綴。 ```java String blogName = "howtodoinjava.com"; blogName.startsWith( "d", 5 ); //true blogName.startsWith( "e", 5 ); //false ``` 7. [`boolean startsWith(String prefix)`](https://howtodoinjava.com/java/string/java-string-startswith-example/) – 測試字符串是否已指定`prefix`,如果是,則返回`true`,否則返回`false`。 在此重載方法中,偏移索引值為 0。 ```java String blogName = "howtodoinjava.com"; blogName.startsWith( "h" ); //true blogName.startsWith( "e" ); //false ``` 8. [`boolean endsWith(String subfix)`](https://howtodoinjava.com/java/string/java-string-endswith-method/) – 查字符串是否以指定的后綴結尾。 ```java String blogName = "howtodoinjava.com"; blogName.endsWith( "com" ); //true blogName.endsWith( "java" ); //false ``` 9. [`int hashCode()`](https://howtodoinjava.com/java/string/string-hashcode-method/) – 返回字符串的哈希碼。 ```java String blogName = "howtodoinjava.com"; blogName.hashCode(); //1894145264 ``` 0. [`int indexOf(int ch)`](https://howtodoinjava.com/java/string/java-string-indexof-method-example/) – 返回指定字符參數在字符串中首次出現的索引。 ```java String blogName = "howtodoinjava.com"; blogName.indexOf( 'o' ); //1 ``` 1. [`int indexOf(int ch, int fromIndex)`](https://howtodoinjava.com/java/string/java-string-indexof-method-example/) – `indexOf(char ch)`方法的重載版本,但是它開始從指定的`fromIndex`中搜索字符串。 ```java String blogName = "howtodoinjava.com"; blogName.indexOf( 'o', 5 ); //6 ``` 2. [`int indexOf(String str)`](https://howtodoinjava.com/java/string/java-string-indexof-method-example/) – 返回指定子字符串`str`首次出現的索引。 ```java String blogName = "howtodoinjava.com"; blogName.indexOf( "java" ); //9 ``` 3. [`int indexOf(String str, int fromIndex)`](https://howtodoinjava.com/java/string/java-string-indexof-method-example/) - `indexOf(String str)`方法的重載版本,但是它開始從指定的`fromIndex`中搜索字符串。 ```java String blogName = "howtodoinjava.com"; blogName.indexOf( "java" , 5); //9 ``` 4. [`int lastIndexOf(int ch)`](https://howtodoinjava.com/java/string/string-lastindexof-method/) – 返回字符串中字符`'ch'`的最后一次出現。 ```java String blogName = "howtodoinjava.com"; blogName.lastIndexOf( 'o' ); //15 ``` 5. [`int lastIndexOf(int ch,int fromIndex)`](https://howtodoinjava.com/java/string/string-lastindexof-method/) – `lastIndexOf(int ch)`方法的重載版本。 從`fromIndex`開始向后搜索。 ```java String blogName = "howtodoinjava.com"; blogName.lastIndexOf( 'o', 5 ); //4 ``` 6. [`int lastIndexOf(String str)`](https://howtodoinjava.com/java/string/string-lastindexof-method/) – 返回最后一次出現的字符串`str`的索引。 與`lastIndexOf(int ch)`相似。 ```java String blogName = "howtodoinjava.com"; blogName.lastIndexOf( "java" ); //9 ``` 7. [`int lastIndexOf(String str, int fromIndex)`](https://howtodoinjava.com/java/string/string-lastindexof-method/) – `lastIndexOf(String str)`方法的重載版本。 從`fromIndex`開始向后搜索。 ```java String blogName = "howtodoinjava.com"; blogName.lastIndexOf( "java", 6 ); //9 ``` 8. [`String substring(int beginIndex)`](https://howtodoinjava.com/java/string/java-string-substring-example/) – 返回字符串的子字符串。 子字符串以指定索引處的字符開頭。 ```java String blogName = "howtodoinjava.com"; blogName.substring( 7 ); //injava.com ``` 9. [`String substring(int beginIndex, int endIndex)`](https://howtodoinjava.com/java/string/java-string-substring-example/) – 返回子字符串。 子字符串以`beginIndex`處的字符開頭,以`endIndex`處的字符結尾。 ```java String blogName = "howtodoinjava.com"; blogName.substring( 7, 9 ); //in ``` 0. [`String concat(String str)`](https://howtodoinjava.com/java/string/java-string-concat-method-example/) – 在字符串的末尾連接指定的字符串參數。 ```java String blogName = "howtodoinjava.com"; blogName.concat( " Hello Visitor !!" ); //howtodoinjava.com Hello Visitor !! ``` 1. [`String replace(char oldChar, char newChar)`](https://howtodoinjava.com/java/string/java-string-replace-method/) – 使用`newChar`參數更改所有出現的`oldChar`之后,返回新的更新字符串。 ```java String blogName = "howtodoinjava.com"; blogName.replace( 'o', 'O' ); //hOwtOdOinjava.cOm ``` 2. [`public String replace(CharSequence target, CharSequence replacement)`](https://howtodoinjava.com/java/string/java-string-replace-method/) – 使用`replacement`參數更改所有出現的`target`后,返回新的更新字符串。 ```java String blogName = "howtodoinjava.com"; blogName.replace( "com", "COM" ); //howtodoinjava.COM ``` 3. [`String replaceFirst(String regex, String replacement)`](https://howtodoinjava.com/java/string/java-string-replacefirst-example/) – 用指定的替換字符串替換與給定[正則表達式](https://howtodoinjava.com/java-regular-expression-tutorials/)參數匹配的子字符串的第一個匹配項。 ```java String blogName = "howtodoinjava.com"; blogName.replaceFirst("how", "HOW"); //HOWtodoinjava.com ``` 4. [`String.replaceAll(String regex, String replacement)`](https://howtodoinjava.com/java/string/java-string-replaceall-example/) – 用替換字符串替換所有出現的與正則表達式參數匹配的子字符串。 5. [`String[] split(String regex, int limit)`](https://howtodoinjava.com/java/string/java-string-split-example/) – 拆分字符串并返回與給定正則表達式匹配的子字符串數組。 `limit`是數組中元素的最大數量。 ```java String blogName = "howtodoinjava.com"; blogName.split("o", 3); //[h, wt, doinjava.com] ``` 6. [`String[] split(String regex)`](https://howtodoinjava.com/java/string/java-string-split-example/) – 先前方法的重載,沒有任何閾值限制。 7. [`boolean contains(CharSequence s)`](https://howtodoinjava.com/java/string/java-string-contains-example/) – 檢查字符串是否包含指定的`char`值序列。 如果是,則返回`true`,否則返回`false`。 如果參數為`null`,則拋出 [`NullPointerException`](https://howtodoinjava.com/java/exception-handling/how-to-effectively-handle-nullpointerexception-in-java/)。 ```java String blogName = "howtodoinjava.com"; blogName.contains( "java" ); //true blogName.contains( "python" ); //false ``` 8. [`public String toUpperCase(Locale locale)`](https://howtodoinjava.com/java/string/java-string-touppercase-method/) – 使用指定語言環境定義的規則將字符串轉換為大寫字符串。 ```java String blogName = "howtodoinjava.com"; blogName.toUpperCase( Locale.getDefault() ); //HOWTODOINJAVA.COM ``` 9. [`String.toUpperCase()`](https://howtodoinjava.com/java/string/java-string-touppercase-method/) – 先前的`toUpperCase()`方法的重載版本,帶有默認語言環境。 0. [`String toLowerCase(Locale locale)`](https://howtodoinjava.com/java/string/java-string-tolowercase-method/) – 使用給定語言環境定義的規則將字符串轉換為小寫字符串。 1. [`String.toLowerCase()`](https://howtodoinjava.com/java/string/java-string-tolowercase-method/) – 具有默認語言環境的先前方法的重載版本。 2. [`String.intern()`](https://howtodoinjava.com/java/string/java-string-intern-method-example/) – 在內存池中搜索指定的字符串,如果找到,則返回它的引用。 否則,此方法將在字符串池中分配創建字符串字面值并返回引用。 3. `boolean isEmpty()` – 如果給定的字符串長度為 0,則返回`true`,否則返回`false`。 ```java String blogName = "howtodoinjava.com"; blogName.isEmpty(); //false "".isEmpty(); //true ``` 4. `static String join()` - 使用指定的分隔符連接給定的字符串,并返回連接的 Java `String`字面值。 ```java String.join("-", "how","to", "do", "in", "java") //how-to-do-in-java ``` 5. `static String format()` – 返回格式化的字符串。 6. `String.trim()` - 從 Java 字符串中刪除開頭和結尾的空格。 7. `char[] toCharArray()` – 將字符串轉換為字符數組。 8. `static String copyValueOf(char[] data)` – 返回一個字符串,其中包含指定字符數組的字符。 ```java char[] chars = new char[] {'h','o','w'}; String.copyValueOf(chars); //how ``` 9. `byte[] getBytes(String charsetName)` – 使用指定的字符集編碼將字符串轉換為字節序列。 0. `byte [] getBytes()` – 先前方法的重載版本。 它使用默認字符集編碼。 1. `int length()` – 返回字符串的長度。 2. `boolean match(String regex)` – 驗證字符串是否與指定的正則表達式參數匹配。 3. `int codePointAt(int index)` – 與`charAt()`方法相似。 它返回指定索引的 Unicode 代碼點值,而不是字符本身。 4. `static String copyValueOf(char[] data, int offset, int count)` – 先前方法的重載版本,帶有兩個額外的參數 – 子數組的初始偏移量和子數組的長度。 它根據額外的參數從數組中選擇字符,然后創建字符串。 5. `getChars(int srcBegin, int srcEnd, char [] dest, int destBegin)` – 將`src`數組的字符復制到`dest`數組。 僅將指定范圍復制(從`srcBegin`到`srcEnd`)到`dest`子數組(從`destBegin`開始)。 6. `static String valueOf()` – 返回所傳遞參數的字符串表示形式,例如`int`,`long`,`float`,`double`,`char`和`char`數組。 7. `boolean contentEquals(StringBuffer sb)` – 將字符串與指定的字符串緩沖區進行比較。 8. `boolean regionMatches(int srcoffset, String dest, int destoffset, int len)` – 將輸入的子字符串與指定字符串的子字符串進行比較。 9. `boolean regionMatches(boolean ignoreCase, int srcoffset, String dest, int destoffset, int len)` – `regionMatches`方法的另一個變體,帶有額外的布爾值參數,用于指定比較是區分大小寫還是不區分大小寫。 ## 3\. 字符串轉換示例 1. [將 Java 字符串轉換為`int`](https://howtodoinjava.com/java/string/convert-string-to-int/) 2. [在 Java 中將`int`轉換為字符串](https://howtodoinjava.com/java/string/convert-int-to-string/) 3. [將字符串轉換為長](https://howtodoinjava.com/java/string/convert-string-to-long/) 4. [在 Java 中將`Long`轉換為字符串](https://howtodoinjava.com/java/string/convert-long-to-string/) 5. [將字符串轉換為日期](https://howtodoinjava.com/java/date-time/java-parse-string-to-date/) 6. [將日期轉換為字符串](https://howtodoinjava.com/java/date-time/java-date-examples/) 7. [將字符串轉換為`String[]`示例](https://howtodoinjava.com/array/string-to-string-array/) 8. [Java 8 – 連接字符串數組 – 將數組轉換為字符串](https://howtodoinjava.com/java8/java-8-join-string-array-example/) 9. [將字符串轉換為`InputStream`示例](https://howtodoinjava.com/java/io/convert-string-to-inputstream-in-java/) 10. [將`InputStream`轉換為字符串示例](https://howtodoinjava.com/java/io/how-to-read-data-from-inputstream-into-string-in-java/) 11. [Java 拆分 CSV 字符串 – 將字符串轉換為列表示例](https://howtodoinjava.com/java/string/java-split-csv-string-to-list/) 12. [將 CSV 連接到字符串](https://howtodoinjava.com/java8/java-8-string-join-csv-example/) 13. [將 HTML 轉義為字符串示例](https://howtodoinjava.com/java/string/unescape-html-to-string/) 14. [轉義 HTML – 將字符串編碼為 HTML 示例](https://howtodoinjava.com/java/string/escape-html-encode-string/) 15. [將字節數組轉換為字符串](https://howtodoinjava.com/array/java-convert-byte-array-to-string-example/) 16. [`StackTrace`到字符串的轉換](https://howtodoinjava.com/java/string/convert-stacktrace-to-string/) 17. [將浮點數轉換為字符串 – 格式轉換為 N 個小數點](https://howtodoinjava.com/java/string/float-to-string-format-decimal-points/) ## 4\. 有用的字符串示例 1. [使用遞歸反轉 Java 中的字符串](https://howtodoinjava.com/java/string/reverse-string-using-recursion/) 2. [刪除單詞之間的多余空格](https://howtodoinjava.com/java/string/remove-extra-whitespaces-between-words/) 3. [僅刪除字符串的前導空格](https://howtodoinjava.com/java/string/remove-leading-whitespaces/) 4. [僅刪除字符串的結尾空格](https://howtodoinjava.com/java/string/trim-remove-trailing-spaces/) 5. [如何在 Java 中反轉字符串](https://howtodoinjava.com/puzzles/how-to-reverse-string-in-java/) 6. [用 Java 反轉字符串中的單詞](https://howtodoinjava.com/java/string/reverse-words-in-string/) 7. [Java 中使用遞歸的反向字符串](https://howtodoinjava.com/java/string/reverse-string-using-recursion/) 8. [如何在字符串中查找重復的單詞](https://howtodoinjava.com/java/string/how-to-find-duplicate-words-in-a-string-in-java/) 9. [如何在字符串中查找重復的字符](https://howtodoinjava.com/java/string/find-duplicate-characters/) 10. [Java 按字母順序對字符串字符進行排序](https://howtodoinjava.com/sort/sort-string-chars-alphabetically/) 11. [將字符串轉換為標題大小寫](https://howtodoinjava.com/java/string/convert-string-to-titlecase/) 12. [分割字符串的 4 種方法](https://howtodoinjava.com/java/string/4-ways-to-split-tokenize-strings-in-java/) 13. [左,右或居中對齊字符串](https://howtodoinjava.com/java/string/how-to-left-right-or-center-align-string-in-java/) 14. [讀取文件為字符串](https://howtodoinjava.com/java/io/java-read-file-to-string-examples/) 15. [Java 8 `StringJoiner`示例](https://howtodoinjava.com/java/string/java8-stringjoiner-example/) 16. [用空格或零左移字符串](https://howtodoinjava.com/java/string/left-pad-string-with-spaces-zeros/) 17. [用空格或零右填充字符串](https://howtodoinjava.com/java/string/right-pad-string-with-spaces-zeros/) 18. [獲取字符串的前 4 個字符](https://howtodoinjava.com/java/string/get-first-4-characters/) 19. [獲取字符串的后 4 個字符](https://howtodoinjava.com/java/string/get-last-4-characters/) 20. [將字符串格式設置為`(123)456-7890`模式](https://howtodoinjava.com/java/string/format-phone-number/) ## 5\. 常見問題 1. [始終使用`length()`而不是`equals()`來檢查空字符串](https://howtodoinjava.com/java/string/always-use-length-instead-of-equals-to-check-empty-string-in-java/) 2. [為什么字符串是不可變的](https://howtodoinjava.com/java/string/java-interview-question-why-strings-are-immutable/) 3. [Java 字符串面試問題](https://howtodoinjava.com/interview-questions/interview-stuff-about-string-class-in-java/) ## 6\. 參考 [`String` Java 文檔](https://docs.oracle.com/javase/9/docs/api/java/lang/String.html)
                  <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>

                              哎呀哎呀视频在线观看