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


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


javax.naming.CompoundName類的getSuffix()方法用於獲取複合名稱對象,該複合名稱對象的組件由該複合名稱中的組件後綴組成。我們需要從此複合名稱對象開始提取後綴的位置作為參數傳遞。返回的複合名稱對象和此複合名稱對象具有相同的語法。如果我們對該複合名稱進行了任何更改,而不會影響返回的名稱,反之亦然。

用法:

public Name getSuffix(int posn)

參數:此方法接受posn,它是要從其開始的組件的從0開始的索引。必須在[0,size()]範圍內。



返回值:此方法返回由[posn,size())範圍內的索引處的組件組成的複合名稱。如果posn等於size(),則返回一個空的化合物名稱。

異常:如果posn在指定範圍之外,則此方法引發ArrayIndexOutOfBoundsException。

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

// Java program to demonstrate 
// CompoundName.getSuffix() 
  
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 getSuffix() 
        CompoundName newCompoundName 
            = (CompoundName) 
                  CompoundName1.getSuffix(3); 
  
        // print value 
        System.out.println( 
            "New CompoundName Object:"
            + newCompoundName); 
    } 
}
輸出:
New CompoundName Object:4@5@6@7

程序2:

// Java program to demonstrate 
// CompoundName.getSuffix() 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 getSuffix() 
        CompoundName newCompoundName 
            = (CompoundName) 
                  CompoundName1.getSuffix(8); 
  
        // print value 
        System.out.println( 
            "New CompoundName Object:"
            + newCompoundName); 
    } 
}
輸出:
New CompoundName Object:x/f

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




相關用法


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