javaarraysstring

Building a string with array values


I have the following code which splits a string and gets the four values separated by a delimiter.

I want to build a result string like this:

/result = "<div class=\"parent\"> <div class=\"heading\">"+arr[0]+"</div> <div class=\"heading\">"+arr[1]+"</div></div><div class=\"parent\"> <div class=\"resultname\">"+arr[2]+"</div> <div class=\"resultname\">"+arr[3]+"</div></div>";

And I don't want to hardcode the array like arr[0], arr[1], etc. I was wondering if there's a better way to handle this. The for loop I've defined in the later portion of below code is definitely not the correct way to handle this.

public class StringExtraction {
    public static void main(String[] args) {
        String str = "|555|777|AB|CD";
        String[] arr = str.substring(1).split("\\|");

        String result = "";
        //result = "<div class=\"parent\"> <div class=\"heading\">"+arr[0]+"</div> <div class=\"heading\">"+arr[1]+"</div></div><div class=\"parent\"> <div class=\"resultname\">"+arr[2]+"</div> <div class=\"resultname\">"+arr[3]+"</div></div>";

        System.out.println("Array length is :-> " + arr.length);
        for (String s1 : arr) {
            System.out.println("Value " + s1 + " and its length is " + s1.length());
        }

        for (int i = 0; i < arr.length; i++) {
            result = "<div class=\"parent\"> <div class=\"heading\">" + arr[i] + "</div> <div class=\"heading\">" + arr[i] + "</div></div><div class=\"parent\"> <div class=\"resultname\">" + arr[i] + "</div> <div class=\"resultname\">" + arr[i] + "</div></div>";
        }

        System.out.println("Print Result below:");
        System.out.println(result);
    }
}

Solution

  • You could put the possible HTML strings into an array as well, add %s to each HTML string in place of their value, and then format() each HTML string to replace the format specifier with the actual value.

    Your code could look something like this:

    public class StringExtraction {
        public static void main(String[] args) {
            String strValues = "|555|777|AB|CD";
            String[] vetValues = strValues.substring(1).split("\\|");
            String[] vetHTML = new String[]{
                    "<div class=\"parent\"> <div class=\"heading\">%s",
                    "</div> <div class=\"heading\">%s",
                    "</div></div><div class=\"parent\"> <div class=\"resultname\">%s",
                    "</div> <div class=\"resultname\">%s</div></div>"
            };
            
            for (int i = 0; i < vetValues.length; i++) {
                vetHTML[i] = String.format(vetHTML[i], vetValues[i]);
            }
    
            String result;
            //result = "<div class=\"parent\"> <div class=\"heading\">"+vetValues[0]+"</div> <div class=\"heading\">"+vetValues[1]+"</div></div><div class=\"parent\"> <div class=\"resultname\">"+vetValues[2]+"</div> <div class=\"resultname\">"+vetValues[3]+"</div></div>";
            result = String.join("", vetHTML);
    
            System.out.println("Print Result below:");
            System.out.println(result);
        }
    }
    

    Here is also a demo at OneCompiler.