CompoundName endsWith() method in Java with Examples

The endsWith() method of a javax.naming.CompoundName class is used to check whether compound name which is passed as a parameter is a suffix of this compound name or not. A compound name ‘X’ is a suffix of this compound name if this compound name object ends with ‘X’. If X is null or not a compound name object, false is returned.
Syntax:
public boolean endsWith(Name n)
Parameters: This method accepts n which is the possibly null compound name to check.
Return value: This method returns true if n is a CompoundName and is a suffix of this compound name, false otherwise.
Below programs illustrate the CompoundName.endsWith() method:
Program 1:
// 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
Program 2:
// 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
References: https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#endsWith(javax.naming.Name)



