📜  Java中的 CompoundName endsWith() 方法及示例

📅  最后修改于: 2022-05-13 01:54:55.044000             🧑  作者: Mango

Java中的 CompoundName endsWith() 方法及示例

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

句法:

public boolean endsWith(Name n)

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

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

下面的程序说明了 CompoundName.endsWith() 方法:
方案一:

// Java program to demonstrate
// CompoundName.endsWith()
  
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(
                "a:b:z:y:x", props);
        CompoundName CompoundName2
            = new CompoundName(
                "z:y:x", props);
  
        // apply endsWith()
        boolean endWithFlag
            = CompoundName1
                  .endsWith(CompoundName2);
  
        // print value
        if (endWithFlag)
            System.out.println(
                "CompoundName1 ends with "
                + " CompoundName2");
        else
            System.out.println(
                "CompoundName1 not ends with "
                + " CompoundName2");
    }
}
输出:
CompoundName1 ends with  CompoundName2

方案二:

// Java program to demonstrate
// CompoundName.endsWith() 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(
                "z/y/x",
                props);
  
        // apply endsWith()
        boolean endWithFlag
            = CompoundName1
                  .endsWith(CompoundName2);
  
        // print value
        if (endWithFlag)
            System.out.println(
                "CompoundName1 ends with "
                + " CompoundName2");
        else
            System.out.println(
                "CompoundName1 not ends with "
                + " CompoundName2");
    }
}
输出:
CompoundName1 not ends with  CompoundName2

参考资料:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#endsWith(javax.naming.Name)