Evangeline Lydia
Evangeline Lydia

Reputation: 21

How to write a function for concatenation in Java

I am new to java. I java a string like this .

String result = "'" + city + "'" + "," + "'" + locality + "'" + "," + "'" + Name_of_Person + "'" + "," + "'"
                + User_email_address + "'" + "," + "'" + user_phone_number + "'" + "," + "'" + sub_locality + "'"
                + "," + "'" + street_name + "'" + "," + "'" + home_plot_no + "'" + "," + "'" + pin_code + "'" + ","
                + "'" + project_society_build_name + "'" + "," + "'" + landmark_reference_1 + "'" + "," + "'"
                + landmark_reference_2 + "'" + "," + "'" + No_of_Schools + "'" + "," + "'" + No_of_Hospitals + "'"
                + "," + "'" + No_of_Metro + "'" + "," + "'" + No_of_Mall + "'" + "," + "'" + No_of_Park + "'" + ","
                + "'" + Distance_of_schools + "'" + "," + "'" + Distance_of_Hospitals + "'" + "," + "'"
                + Distance_of_Metro + "'" + "," + "'" + Distance_of_Mall + "'" + "," + "'" + Distance_of_Park + "'"
                + "," + "'" + lat + "'" + "," + "'" + lng + "'" + "," + "'" + ip + "'" + "," + "'" + hostname + "'"
                + "," + "'" + ip_city + "'" + "," + "'" + ip_region + "'" + "," + "'" + ip_country + "'" + "," + "'"
                + ip_loc + "'" + "," + "'" + ip_org + "'" + "," + "'" + ip_postal + "'" + "," + "'" + home_type
                + "'" + "," + "'" + area + "'" + "," + "'" + beds + "'" + "," + "'" + bath_rooms + "'" + "," + "'"
                + building_age + "'" + "," + "'" + floors + "'" + "," + "'" + balcony + "'" + "," + "'" + amenities
                + "'" + "," + "'" + gated_security + "'" + "," + "'" + physical_security + "'" + "," + "'"
                + cctv_camera + "'" + "," + "'" + controll_access + "'" + "," + "'" + elevator + "'" + "," + "'"
                + power_back_up + "'" + "," + "'" + parking + "'" + "," + "'" + partial_parking + "'" + "," + "'"
                + onsite_maintenance_store + "'" + "," + "'" + open_garden + "'" + "," + "'" + party_lawn + "'"
                + "," + "'" + amenities_balcony + "'" + "," + "'" + club_house + "'" + "," + "'" + fitness_center
                + "'" + "," + "'" + swimming_pool + "'" + "," + "'" + party_hall + "'" + "," + "'" + tennis_court
                + "'" + "," + "'" + basket_ball_court + "'" + "," + "'" + squash_coutry + "'" + "," + "'"
                + amphi_theatre + "'" + "," + "'" + business_center + "'" + "," + "'" + jogging_track + "'" + ","
                + "'" + convinience_store + "'" + "," + "'" + guest_rooms + "'" + "," + "'" + interior + "'" + ","
                + "'" + tiles + "'" + "," + "'" + marble + "'" + "," + "'" + wooden + "'" + "," + "'"
                + modular_kitchen + "'" + "," + "'" + partial_modular_kitchen + "'" + "," + "'" + gas_pipe + "'"
                + "," + "'" + intercom_system + "'" + "," + "'" + air_conditioning + "'" + "," + "'"
                + partial_air_conditioning + "'" + "," + "'" + wardrobe + "'" + "," + "'" + sanitation_fixtures
                + "'" + "," + "'" + false_ceiling + "'" + "," + "'" + partial_false_ceiling + "'" + "," + "'"
                + recessed_lighting + "'" + "," + "'" + location + "'" + "," + "'" + good_view + "'" + "," + "'"
                + transporation_hub + "'" + "," + "'" + shopping_center + "'" + "," + "'" + hospital + "'" + ","
                + "'" + school + "'" + "," + "'" + ample_parking + "'" + "," + "'" + park + "'" + "," + "'" + temple
                + "'" + "," + "'" + bank + "'" + "," + "'" + less_congestion + "'" + "," + "'" + less_pollution
                + "'" + "," + "'" + maintenance + "'" + "," + "'" + maintenance_value + "'" + "," + "'"
                + Near_by_school + "'" + "," + "'" + Near_by_hospital + "'" + "," + "'" + Near_by_mall + "'" + ","
                + "'" + Near_by_park + "'" + "," + "'" + Near_by_metro + "'" + "," + "'" + city + "'" + "," + "'"
                + locality + "'" + "," + "'" + token + "'";

I concatenated many strings to one result string variable. I need to write function for this concatenation operation for code optimization.any help will be appreciated.

Upvotes: 0

Views: 67

Answers (2)

Jan
Jan

Reputation: 13858

One simple approach would be to use a StringJoiner

//Setup the joiner for between items
StringJoiner sj = new StringJoiner("','");
sj.add(city);
sj.add(locality);
//[...]

//One last wrap to add initial and trailing '
String result = new StringBuilder("'").append(sj.toString()).append("'").toString();

However, you need to take care that the strings you add are properly escaped (' inside your strings might break your code)

If you run Java 7 still, you'd be forced to do it with StringBuilder alone:

public String join(String... s) {
    StringBuilder sb = new StringBuilder("'");
    for(String token : s) {
        sb.append(token).append("','");
    }
    sb.append("'");
    return sb.toString();
}

And call it like

String result = join(city, locality, Name_of_Person); //Need to add more...

Upvotes: 1

user4910279
user4910279

Reputation:

Try this.

    Object[] all = {
        city, locality, Name_of_Person, User_email_address, user_phone_number,
        sub_locality, street_name, home_plot_no, pin_code, project_society_build_name,
        // .....
    };
    String result = Stream.of(all)
        .map(x -> "'" + x + "'")
        .collect(Collectors.joining(","));
    System.out.println(result);

Or

static String join(Object... objects) {
    StringBuilder sb = new StringBuilder();
    for (Object e : objects)
        sb.append(",").append("'").append(e).append("'");
    return sb.substring(1);
}

and

String result = join(all);

Upvotes: 0

Related Questions