首頁  >  文章  >  Java  >  Java 中的 substring() 函數

Java 中的 substring() 函數

王林
王林原創
2024-08-30 15:35:52819瀏覽

Java 是最廣為人知和使用的程式語言之一,提供了廣泛的函數和方法。任何程式語言的主要方面之一是可用資料類型的清單。 Java 將資料類型分為原始資料類型和非原始資料類型,其中 String 是非原始資料類型,因為它指的是物件。該 String 物件具有多種方法來對字串執行各種操作。其中一種方法是 java 中的 substring() 函數。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

substring(): 字串的一部份是子字串。 substring() 方法傳回指定字串的部分。 substring 方法傳回一個新字串,該字串是該字串的子字串。

現在我們已經了解字串和子字串方法是什麼,讓我們了解一下子字串方法的標準語法。

文法:

public String substring(int beginIndex)

這將傳回一個全新的字串,它是傳遞的字串的子字串。它所採用的參數是 beginIndex,它指定要從何處選取 substring() 的索引點。從指定索引的字元開始到字串末尾。

例外: 如果傳遞負索引或任何大於字串原始長度的索引,則會拋出「IndexOutOfBoundsException」。

substring() 函數在 Java 中如何運作?

當呼叫java中的substring()函數時,它會根據傳遞的beginIndex參數建立一個新字串,該字串是原始字串的一部分並傳回它。當 beginIndex 被傳遞時,它會在字串中搜尋指定的索引值,然後選擇指定索引處存在的字元。在一個版本中,傳遞 beginIndex,而在 substring 方法的另一個版本中,提供 beginIndex 和 endIndex。重要的是要理解這樣一個事實:每次呼叫 substring 方法時,它都會建立一個新字串,因為字串資料類型是不可變的。

實作 substring() 函數的範例

讓我們看看下面給出的在 java 中實作 substring() 函數的範例:

範例#1

代碼:

public class java_subs {
public static void main(String args[]) {
String sampleStr = new String("This is Java substring()");
System.out.print("The original substring: " +sampleStr);
System.out.print("\n Our new substring is : ");
System.out.println(sampleStr.substring(8));
}
}

程式碼說明:我們的第一個範例程式有一個簡單的演示子字串方法,並帶有起始索引參數。我們創建了我們的類,然後添加了我們的主類。聲明我們的字串資料類型及其變數名稱和值。首先,我們將按原樣列印原始字串。然後我們將使用 substring 方法的效果列印新字串。我們已將「beginIndex」指定為從第8th 位置開始,這表示我們的新子字串將從第8th 索引開始,從第8 th 中選取字元🎜> 字串到最後的位置。執行上述程式碼後,子字串應為「Java substring()」。請參閱下面所附的輸出螢幕截圖。

輸出:

Java 中的 substring() 函數

如您所見,我們首先按原樣列印原始字串,然後列印子字串。

範例#2

我們現在將看到另一個範例,與第一個範例類似,但添加了參數「endIndex」。

代碼:

public class java_subs {
public static void main(String args[]) {
String sampleStr = new String("This is Java substring() and something extra.");
System.out.print("The original substring: " +sampleStr);
System.out.print("\n Our new substring: ");
System.out.println(sampleStr.substring(8, 24));
}
}

程式碼說明:與之前的程式碼類似,我們有我們的類別和主類別。然後我們聲明了我們的字串,其中包含變數名和值,與之前相比,它相當長。接下來,我們列印了原始字串,然後列印了新字串。透過我們的 substring 方法,我們傳遞了兩個參數,beginIndex 和 endIndex,即 8 和 24。這些索引定義了子字串必須從哪個點開始以及在哪裡結束。在我們的範例中,子字串將從第8 個 索引開始,選取第8 個 索引處的任何字符,直到第24 個 索引。因此,我們的新子字串將位於第8th 和24th 索引之間,在第8th 之前和第24th 索引之後。執行上述程式碼後,子字串應為「Java substring()」。

輸出:

Java 中的 substring() 函數

如您在輸出中所看到的,第 8 個 之前和第 24 個 之後的字元位置不是我們新建立的子字串的一部分。最廣泛使用的子字串方法之一是從開頭和結尾清除不必要的字元。

範例 #3

代碼:

public class java_subs {
public static void main(String args[]) {
String sampleStr = new String("This is just another example of java substring.");
System.out.print("The original substring: " +sampleStr);
int slength = sampleStr.length();
System.out.print("\n Our new substring: ");
System.out.println(sampleStr.substring(2, slength-9));
}
}

Code Explanation: In our third example, we have our class and main class within. We have a slightly different string here, and we have used the length method. Similar to earlier examples, we have printed our original string and then declared a new integer with slength, and the value is the length of the string. Then in our final print statement, our beginIndex is 2, meaning the substring will begin from the 2nd index, and the endIndex is slength -9, meaning whatever the length of the string is, the substring will end on 9 minus the original length. In our example, slength -9 will result in a substring to end on “s”.

Output:

Java 中的 substring() 函數

One of the important aspects to remember is, the original string is very long and holds the 1Gb size of an array. So, even if the substring being very small, it will be of 1GB. Though this will obviously stop the original string from being garbage collected, it is a case of memory leak, meaning a large amount of memory is acquired even though it won’t be used.

Conclusion

The string is one of the important data types, and the substring method simply returns a part of the original string. When the substring method is called, it creates a new string with an offset and count. We have demonstrated multiple examples with various methods to learn about substring. Code with its respective screenshots are attached.

以上是Java 中的 substring() 函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn