📌  相关文章
📜  Java中的 CompoundName toString() 方法及示例

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

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

javax.naming.CompoundName 类toString()方法用于创建此复合名称对象的字符串表示,使用通过属性传递的复合名称的语法。如果组件为空,则它由一个空字符串表示。创建的非空复合名称对象的字符串表示具有相同的语法属性,并且所有组件在字符串表示中由该语法分隔。

句法:

public String toString()

参数:此方法不接受任何内容。

返回值:此方法返回此复合名称的非空字符串表示形式。

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

// Java program to demonstrate
// CompoundName.toString()
  
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 toString()
        String toString
            = CompoundName1.toString();
  
        // print value
        System.out.println("toString: "
                           + toString);
    }
}
输出:
toString: 1@2@3@4@5@6@7

方案二:

// Java program to demonstrate
// CompoundName.toString() 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 toString()
        String toString
            = CompoundName1.toString();
  
        // print value
        System.out.println("CompoundName:"
                           + toString);
    }
}
输出:
CompoundName:c/e/d/v/a/b/z/y/x/f

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