📜  Java中的 CompositeName startsWith() 方法和示例

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

Java中的 CompositeName startsWith() 方法和示例

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

句法:

public boolean startsWith(Name n)

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

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

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

// Java program to demonstrate
// CompositeName.startsWith()
  
import java.util.Properties;
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
  
public class GFG {
    public static void main(String[] args)
        throws InvalidNameException
    {
  
        // create composite name object
        CompositeName CompositeName1
            = new CompositeName("1:2:4:6");
        CompositeName CompositeName2
            = new CompositeName("2:4:6");
  
        // apply startsWith()
        boolean Flag
            = CompositeName1
                  .startsWith(CompositeName2);
  
        // print value
        if (Flag)
            System.out.println(
                "CompositeName1 starts with "
                + " CompositeName2");
        else
            System.out.println(
                "CompositeName1 not starts with "
                + " CompositeName2");
    }
}
输出:
CompositeName1 not starts with  CompositeName2

方案二:

// Java program to demonstrate
// CompositeName.startsWith() method
  
import java.util.Properties;
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
  
public class GFG {
    public static void main(String[] args)
        throws InvalidNameException
    {
  
        // create composite name object
        CompositeName CompositeName1
            = new CompositeName(
                "ca/eaaa/daaa/vaaa/a/b/z/y/x/f");
        CompositeName CompositeName2
            = new CompositeName(
                "ca/eaaa/daaa");
  
        // apply startsWith()
        boolean Flag
            = CompositeName1
                  .startsWith(CompositeName2);
  
        // print value
        if (Flag)
            System.out.println(
                "CompositeName1 starts with "
                + " CompositeName2");
        else
            System.out.println(
                "CompositeName1 not starts with "
                + " CompositeName2");
    }
}
输出:
CompositeName1 starts with  CompositeName2

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