Java StringBuilder offsetByCodePoints() 方法



Java StringBuilder offsetByCodePoints() 方法用于检索此序列中相对于给定索引偏移 codePointOffset 个代码点的索引值

此方法返回序列字符的代码点值的流。流包含整数代码点值,类似于 Java 中的ASCII 值

offsetByCodePoints() 方法接受两个作为整数的参数,分别保存indexcodePointOffset 的值。如果索引值为负或大于序列长度,等等,它会抛出不同的异常。

语法

以下是Java StringBuilder offsetByCodePoints() 方法的语法:

public int offsetByCodePoints(int index, int codePointOffset)

参数

  • index − 要偏移的索引。

  • codePointOffset − 代码点偏移量。

返回值

此方法返回此序列中的索引。

示例:获取代码点偏移量

如果给定的索引值为正小于序列长度,则 offsetByCodePoint() 方法将返回索引值。

在下面的程序中,我们使用值为“TutorialsPoint”创建StringBuilder 类的对象。使用offsetByCodePoints(),我们尝试获取指定startIndex 2codePointOffset 5处的索引值。

public class OffsetCodePoint {
   public static void main(String[] args) {
      //create an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      System.out.println("The given String is: " + sb);
      //initialize the index and codePointOffset values
      int index = 2;
      int codePointOffset = 5;
      System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset);
      //using the offsetByCodePoint() method
      System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset));
   }
}

输出

执行上述程序后,将产生以下结果:

The given String is: TutorialsPoint
The given index and code point offset values are: 2 and 5
The index is: 7

示例:获取代码点偏移量时遇到IndexOutOfBoundsException

如果给定的索引值为负大于序列长度,则 offsetByCodePoint() 方法将抛出 IndexOutOfBoundsException。

在下面的程序中,我们使用值为“Java Programming”实例化StringBuilder 类。使用offsetByCodePoints() 方法,我们尝试获取指定index -1codePointOffset 5处的索引值。

public class OffsetCodePoint {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuilder class
         StringBuilder sb = new StringBuilder("Java Programming");
         System.out.println("The given String is: " + sb);
         //initialize the index and codePointOffset values
         int index = -1;
         int codePointOffset = 5;
         System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset);
         //using the offsetByCodePoint() method
         System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception e: " + e);
      }
   }
}

输出

以下是上述程序的输出:

The given String is: Java Programming
The given index and code point offset values are: -1 and 5
java.lang.IndexOutOfBoundsException
   at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461)
   at java.base/java.lang.StringBuilder.offsetByCodePoints(StringBuilder.java:279)
   at OffsetCodePoint.main(OffsetCodePoint.java:15)
Exception e: java.lang.IndexOutOfBoundsException

示例:获取代码点偏移量时遇到IndexOutOfBoundsException

如果给定的索引值大于序列长度,此方法将抛出IndexOutOfBoundException

在下面的示例中,我们使用值为“Hello”创建一个StringBuilder。使用offsetByCodePoints() 方法,我们尝试获取指定startIndex 10codePointOffset 2处的索引值。

public class OffsetCodePoint {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuilder class
         StringBuilder sb = new StringBuilder("Hello");
         System.out.println("The given String is: " + sb);
         //initialize the index and codePointOffset values
         int index = 10;
         int codePointOffset = 2;
         System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset);
         //using the offsetByCodePoint() method
         System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception e: " + e);
      }
   }
}

输出

上述程序产生以下输出:

The given String is: Hello
The given index and code point offset values are: 10 and 2
java.lang.IndexOutOfBoundsException
   at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461)
   at java.base/java.lang.StringBuilder.offsetByCodePoints(StringBuilder.java:279)
   at OffsetCodePoint.main(OffsetCodePoint.java:15)
Exception e: java.lang.IndexOutOfBoundsException
java_lang_stringbuilder.htm
广告
© . All rights reserved.