
- Java.lang 包類
- Java.lang - 首頁
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包額外內容
- Java.lang - 介面
- Java.lang - 錯誤
- Java.lang - 異常
- Java.lang 包有用資源
- Java.lang - 有用資源
- Java.lang - 討論
Java - String codePointBefore() 方法
描述
Java String codePointBefore() 方法返回指定索引之前字元的程式碼點值(Unicode 程式碼點)。索引指的是給定字串中字元值的位置(Unicode 程式碼單元),範圍從1 到長度。
索引值從1 開始,到字串長度結束,因為如果我們將索引值從0開始,它將開始搜尋零索引值之前的字元值,即-1。在這種情況下,codePointBefore() 方法將丟擲異常。
codePointBefore() 方法接受一個整數引數,該引數儲存索引值,當索引值小於 1 或大於字串長度時,它會丟擲異常。
語法
以下是Java String codePointBefore() 方法的語法:
public int codePointBefore(int index)
引數
- index - 這是應該返回的程式碼點之後的索引。
返回值
此方法返回給定索引之前的 Unicode 程式碼點值。
在字串中檢查 0 索引之前的程式碼點時獲取異常示例
如果給定的索引值為零,則 codePointBefore() 方法會丟擲StringIndexOutOfBoundsException。
在以下示例中,我們使用值"HelloWorld"例項化字串類。然後,使用codePointBefore()方法,我們嘗試檢索指定索引 0之前的字元程式碼點值。
package com.tutorialspoint.String; public class Demo { public static void main(String[] args) { try { //creating an object of the string class String str = new String("HelloWorld"); System.out.println("The string value is: " + str); //initialize the index value int index = 0; System.out.println("The given index value is: " + index); //using the codePointBefore() method System.out.println("The character code point value before specoied index " + index + " is: " + str.codePointBefore(index)); } catch(IndexOutOfBoundsException e) { e.printStackTrace(); System.out.println("Exception: " + e); } } }
輸出
執行上述程式後,將產生以下結果:
The string value is: HelloWorld The given index value is: 0 java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.base/java.lang.String.codePointBefore(String.java:1578) at com.tutorialspoint.String.Demo.main(Demo.java:13) Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
在字串中檢查 -1 索引之前的程式碼點時獲取異常示例
如果給定的索引值為負數,則此方法會丟擲StringIndexOutOfBoundsException。
在此示例中,我們使用值"TutorialsPoint"建立一個字串字面量。然後,使用codePointBefore()方法,我們嘗試檢索指定索引 -1之前的字元程式碼點值。
package com.tutorialspoint.String; public class Test { public static void main(String[] args) { try { // Initialized the string literal String str = "TutorialsPoint"; System.out.println("The string value is: " + str); //initialize the index value int index = -1; System.out.println("The given index values is: " + index); //using the codePointBefore() method System.out.println("The character code point value before the specifed index " + index + " is: " + str.codePointBefore(index)); } catch(IndexOutOfBoundsException e) { e.printStackTrace(); System.out.println("Exception: " + e); } } }
輸出
以下是上述程式的輸出:
The string value is: TutorialsPoint The given index values is: -1 java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.base/java.lang.String.codePointBefore(String.java:1578) at com.tutorialspoint.String.Test.main(Test.java:13) Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
在字串中檢查有效索引之前的程式碼點示例
如果給定的索引值為正數且小於字串長度,則 codePointBefore() 方法返回指定索引之前的字元程式碼點。
在以下程式中,我們使用值"Tutorix"建立 String 類的物件。然後,使用codePointBefore()方法,我們嘗試檢索指定索引 2 和 4之前的字元程式碼點值。
package com.tutorialspoint.String; import java.lang.*; public class Demo { public static void main(String[] args) { // create an object of the string String str = new String("Tutorix"); System.out.println("The string value is: " + str); //initialize the index values int index1 = 2; int index2 = 4; System.out.println("The given index values are: " + index1 + " and " + index2); //using the codePointBefore() method System.out.println("The character code point value before the specified index " + index1 + " is: " + str.codePointBefore(index1)); System.out.println("The character code point value before the specified index " + index2 + " is: " + str.codePointBefore(index2)); } }
輸出
上述程式產生以下結果:
The string value is: Tutorix The given index values are: 2 and 4 The character code point value before the specified index 2 is: 117 The character code point value before the specified index 4 is: 111