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


Java CompoundName startsWith()用法及代码示例


javax.naming.CompoundName类的startsWith()方法用于检查作为参数传递的化合物名称是否为此化合物名称的前缀。如果此复合名称对象以“ X”结尾,则复合名称“ X”是该复合名称的前缀。如果X为null或不是复合名称对象,则返回false。

用法:

public boolean startsWith(Name n)

参数:此方法接受n,n是要检查的可能为空的化合物名称。



返回值:如果n是CompoundName并且是该化合物名称的前缀,则此方法返回true,否则返回false。

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

// Java program to demonstrate 
// CompoundName.startsWith() 
  
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:4:6", props); 
        CompoundName CompoundName2 
            = new CompoundName( 
                "2:4:6", props); 
  
        // apply startsWith() 
        boolean Flag 
            = CompoundName1 
                  .startsWith(CompoundName2); 
  
        // print value 
        if (Flag) 
            System.out.println( 
                "CompoundName1 starts with "
                + " CompoundName2"); 
        else
            System.out.println( 
                "CompoundName1 not starts with "
                + " CompoundName2"); 
    } 
}
输出:
CompoundName1 not starts with  CompoundName2

程序2:

// Java program to demonstrate 
// CompoundName.startsWith() 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); 
        CompoundName CompoundName2 
            = new CompoundName( 
                "c/e/d", 
                props); 
  
        // apply startsWith() 
        boolean Flag 
            = CompoundName1 
                  .startsWith(CompoundName2); 
  
        // print value 
        if (Flag) 
            System.out.println( 
                "CompoundName1 starts with "
                + " CompoundName2"); 
        else
            System.out.println( 
                "CompoundName1 not starts with "
                + " CompoundName2"); 
    } 
}
输出:
CompoundName1 starts with  CompoundName2

参考文献:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#startsWith(javax.naming.Name)




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 CompoundName startsWith() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。