当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。