當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java CompoundName toString()用法及代碼示例

javax.naming.CompoundName類的toString()方法用於使用通過屬性傳遞的複合名稱的語法來創建此複合名稱對象的字符串表示形式。如果組件為空,則用空字符串表示。創建的非空化合物名稱對象的字符串表示形式具有相同的語法屬性,並且所有組件都由該語法在字符串表示形式中分開。

用法:

public String toString()

參數:此方法不接受任何內容。



返回值:此方法返回此複合名稱的非空字符串表示形式。

以下示例程序旨在說明CompoundName.toString()方法:
程序1:

// Java program to demonstrate 
// CompoundName.toString() 
  
import java.util.Properties; 
import javax.naming.CompoundName; 
import javax.naming.InvalidNameException; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InvalidNameException 
    { 
  
        // need properties for CompoundName 
        Properties props = new Properties(); 
        props.put("jndi.syntax.separator", "@"); 
        props.put("jndi.syntax.direction", 
                  "left_to_right"); 
  
        // create compound name object 
        CompoundName CompoundName1 
            = new CompoundName( 
                "1@2@3@4@5@6@7", 
                props); 
  
        // apply toString() 
        String toString 
            = CompoundName1.toString(); 
  
        // print value 
        System.out.println("toString:"
                           + toString); 
    } 
}
輸出:
toString:1@2@3@4@5@6@7

程序2:

// Java program to demonstrate 
// CompoundName.toString() method 
  
import java.util.Properties; 
import javax.naming.CompoundName; 
import javax.naming.InvalidNameException; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InvalidNameException 
    { 
  
        // need properties for CompoundName 
        Properties props = new Properties(); 
        props.put("jndi.syntax.separator", "/"); 
        props.put("jndi.syntax.direction", 
                  "left_to_right"); 
  
        // create compound name object 
        CompoundName CompoundName1 
            = new CompoundName( 
                "c/e/d/v/a/b/z/y/x/f", 
                props); 
  
        // apply toString() 
        String toString 
            = CompoundName1.toString(); 
  
        // print value 
        System.out.println("CompoundName:"
                           + toString); 
    } 
}
輸出:
CompoundName:c/e/d/v/a/b/z/y/x/f

參考文獻:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#toString()




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 CompoundName toString() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。