gpt4 book ai didi

java - 如何在 Java 中编写串联函数

转载 作者:行者123 更新时间:2023-12-01 10:24:39 25 4
gpt4 key购买 nike

我是java新手。我java一个像这样的字符串。

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 + "'";

我将许多字符串连接到一个结果字符串变量。我需要为此串联操作编写函数以优化代码。任何帮助将不胜感激。

最佳答案

一种简单的方法是使用 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();

但是,您需要注意您添加的字符串是否已正确转义(字符串中的 ' 可能会破坏您的代码)

如果您仍然运行 Java 7,您将被迫单独使用 StringBuilder:

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

并称其为

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

关于java - 如何在 Java 中编写串联函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35408194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com