Java - String copyValueOf() 方法



描述

Java String copyValueOf() 方法返回一個表示指定陣列中字元序列的字串。它建立一個新陣列並將字元插入其中。

copyValueOf() 方法是一個靜態方法,這意味著可以直接使用類名本身訪問它。

此方法具有兩個多型變體,具有不同的引數:int, char [ ]。(以下是所有多型變體的語法)。

語法

以下是Java String copyValueOf() 方法的語法:

public static String copyValueOf(char[] data)// first syntax
public static String copyValueOf(char[] data, int offset, int count)// second syntax

引數

  • data − 這是字元陣列。

  • offset − 子陣列的初始偏移量。

  • count − 子陣列的長度。

返回值

此方法返回一個包含字元陣列中字元的字串。

將字元陣列複製到字串示例

如果陣列的字元不為空,則 copyValueOf() 方法返回一個表示 char 陣列中字元的字串。

在下面的程式中,我們建立一個值為'j','a','v' 和 'a' 的 char 陣列。使用copyValueOf() 方法,我們嘗試檢索表示 char 陣列中字元的字串。

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      
      // create an character array
      char ch[] = {'j','a','v','a'};
      System.out.print("The char array elements are: ");
      for(int i = 0; i<ch.length; i++) {
         System.out.print(ch[i] +  " ");
      }
      
      // using copyValueOf() method
      String str = String.copyValueOf(ch);
      
      // prints the string
      System.out.println("\nThe string represents the characters of the char array is: " + str);
   }
}

輸出

執行上述程式後,將產生以下結果:

The char array elements are: j a v a 
The string represents the characters of the char array is: java

使用空字元陣列時 copyValueOf() 方法引發異常示例

如果給定的字元陣列為null,此方法將丟擲NullPointerException

在下面的示例中,我們建立一個值為null 的 char 陣列。使用copyValueOf() 方法,我們嘗試檢索表示給定 char 陣列中字元的字串。由於 char 陣列為空,因此會丟擲異常。

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      try {
         
         // create an character array
         char ch[] = null;
         System.out.print("The char array elements are: ");
         for(int i = 0; i<ch.length; i++) {
            System.out.print(ch[i] +  " ");
         }
         
         // using copyValueOf() method
         String str = String.copyValueOf(ch);
         
         // prints the string
         System.out.println("The string represents the charcaters of the char array is: " + str);
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The char array elements are: java.lang.NullPointerException: Cannot read the array length because "ch" is null
	at com.tutorialspoint.Demo.main(Demo.java:9)
Exception: java.lang.NullPointerException: Cannot read the array length because "ch" is null

使用偏移量使用 copyValueOf() 方法複製字元陣列示例

如果給定的 char 陣列不為空,並且偏移量和計數引數值已傳遞給方法,則 copyValueOf() 方法將返回表示 char 陣列中字元的字串。

在此程式中,我們建立一個值為'H','E','L','L' 和 'O' 的 char 陣列。使用copyValueOf() 方法,我們嘗試檢索表示 char 陣列中字元的字串,其中偏移量值為1,計數值為4

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      try {
         
         // create an character array
         char ch[] = { 'H', 'E', 'L', 'L', 'O'};
         System.out.print("The char array elements are: ");
         for(int i = 0; i<ch.length; i++) {
            System.out.print(ch[i] +  " ");
         }
         
         //initialize the offset and count value
         int offset = 1, count = 4;
         System.out.println("\nThe offset and count values are: " + offset + " and " + count);
         // using copyValueOf() method
         String str = String.copyValueOf(ch, offset, count);
         
         // prints the string
         System.out.println("The string represents the characters of the char array is: " + str);
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

上述程式產生以下輸出:

The char array elements are: H E L L O 
The offset and count values are: 1 and 4
The string represents the characters of the char array is: ELLO	

使用無效偏移量時 copyValueOf() 方法引發異常示例

如果給定的偏移量值為負數,則 copyValueOf() 方法將丟擲StringIndexOutOfBoundsException

在下面的程式中,我們建立一個值為'H','E','L','L' 和 'O' 的 char 陣列。使用copyValueOf() 方法,我們嘗試檢索表示 char 陣列中字元的字串,其中偏移量值為-1,計數值為2。由於偏移量值為負數,因此會丟擲異常。

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      try {
         
         // create an character array
         char ch[] = { 'H', 'E', 'L', 'L', 'O'};
         System.out.print("The char array elements are: ");
         for(int i = 0; i<ch.length; i++) {
            System.out.print(ch[i] +  " ");
         }
         
         //initialize the offset and count value
         int offset = -1, count = 2;
         System.out.println("\nThe offset and count values are: " + offset + " and " + count);
         
         // using copyValueOf() method
         String str = String.copyValueOf(ch, offset, count);
         
         // prints the string
         System.out.println("The specified returned string is: " + str);
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

執行上述程式後,將生成以下輸出:

The char array elements are: H E L L O 
The offset and count values are: -1 and 2
java.lang.StringIndexOutOfBoundsException: offset -1, count 2, length 5
	at java.base/java.lang.String.checkBoundsOffCount(String.java:4589)
	at java.base/java.lang.String.rangeCheck(String.java:304)
	at java.base/java.lang.String.<init>(String.java:300)
	at java.base/java.lang.String.copyValueOf(String.java:4273)
	at com.tutorialspoint.String.Demo.main(Demo.java:17)
Exception: java.lang.StringIndexOutOfBoundsException: offset -1, count 2, length 5
java_lang_string.htm
廣告