Home > other >  How to substitute constant value on html String?
How to substitute constant value on html String?

Time:09-16

public class A { 
 
    String food = "briyani";
    int age = 18;
    String name;
    String description;
    
    A(String name,String description){
        this.name=name;
        this.description =description;
    }

    public A() {}
    
    public static void main(String[] args) {
        A a = new A();
        a.test();
        
    }

    private void test() {
        A a = new A("yogesh",B.DESCRIPTION );
        System.out.println(B.DESCRIPTION );
    }
}

public class B {
    public static String DESCRIPTION = "<!DOCTYPE html><html>"  
            "<br>My name is yogesh. My fav food is %s. And my age is %d " 
            "</html>";
}

DESCRIPTION was an html code I want to send it as email so how can I substitute the values here in the place of %s and %d respectively?

CodePudding user response:

You can do it with String.format() as below:

public static final  String DESCRIPTION = "<!DOCTYPE html><html>"  
      "<br>My name is yogesh. My fav food is %s. And my age is %d " 
      "</html>";

public static void main(String[] args) throws IOException {
    System.out.println(String.format(DESCRIPTION, "Hello", 10));
}

Output:
My name is yogesh. My fav food is Hello. And my age is 10

CodePudding user response:

String newString = DESCRIPTION.replace("%s","replacement"); This replaces all occurrences of "%s" to "replacement".

CodePudding user response:

Class A:

/**
 * The type Http post to node.
 */
public class A {

    private String food;
    private int age;
    private String name;
    private String description;

    /**
     * Instantiates a new A.
     *
     * @param food        the food
     * @param age         the age
     * @param name        the name
     * @param description the description
     */
    public A(String food, int age, String name, String description) {
        this.food = food;
        this.age = age;
        this.name = name;
        this.description = description;
    }

    /**
     * Instantiates a new A.
     */
    public A() {
    }

    /**
     * Gets food.
     *
     * @return the food
     */
    public String getFood() {
        return food;
    }

    /**
     * Sets food.
     *
     * @param food the food
     */
    public void setFood(String food) {
        this.food = food;
    }

    /**
     * Gets age.
     *
     * @return the age
     */
    public int getAge() {
        return age;
    }

    /**
     * Sets age.
     *
     * @param age the age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * Gets name.
     *
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * Sets name.
     *
     * @param name the name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets description.
     *
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * Sets description.
     *
     * @param description the description
     */
    public void setDescription(String description) {
        this.description = description;
    }
}

Class B:

/**
 * The type Http post to node.
 */

class B {
    public final static String DESCRIPTION = "<!DOCTYPE html><html>"  
            "<br>My name is %s. My fav food is %s. And my age is %d " 
            "</html>";
}

Class C:

public class C {
    /**
     * The entry point of application.
     *
     * @param args the input arguments
     */
    public static void main(String[] args) {
        A a = new A("food", 18, "name", null);
        System.out.println(createDescription(B.DESCRIPTION,a));
    }

    private static String createDescription(String description, A a) {
        return String.format(description,a.getFood(),a.getName(),a.getAge());
    }
}

CodePudding user response:

What you're looking for is

String.format(String format, Object... args) 

code like this looks better

class A {
    private String favFood;
    private int age;
    private String name;

    public A(String name, int age,String food) {
        this.name = name;
        this.age = age;
        this.favFood = food;
    }

    public String getDescription() {
        return String.format("<!DOCTYPE html><html>"  
                "<br>My name is %s. My fav food is %s. And my age is %d "  
                "</html>", name, favFood, age);
    }
}

public class Main {
    public static void main(String[] args) {
        A a = new A("yogesh", 18, "briyani");
        String html = a.getDescription();
        System.out.println(html);
    }
}

CodePudding user response:

Simply replace your System.out.println

by System.out.format(B.DESCRIPTION, food, age);.

The entire code will be:

public class A { 
    
    String food = "briyani";
    int age = 18;
    String name;
    String description;
    
    A(String name,String description){
        this.name=name;
        this.description =description;
    }

    public A() {}
    
    public static void main(String[] args) {
        A a = new A();
        a.test();
        
    }

    private void test() {
        A a = new A("yogesh",B.DESCRIPTION );
        System.out.format(B.DESCRIPTION, food, age);
    }
}

class B {
    public static String DESCRIPTION = "<!DOCTYPE html><html>"  
            "<br>My name is yogesh. My fav food is %s. And my age is %d " 
            "</html>";
}

Output :
My name is yogesh. My fav food is briyani. And my age is 18

  •  Tags:  
  • java
  • Related