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

标签 java string concatenation

我是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/

相关文章:

java - 我正在尝试使用 Flowlayout 将 JPanel 添加到 JFrame,但仍然出现异常。我做错了什么?

java - 更新 zest 2 中节点的标签/图标

c - 字符串中的只读变量在 for 循环 C 中不可分配

c++ - 如何将字符串更改为 QString?

字符串操作算法

laravel - 如何在 Laravel 中插入数据库之前连接自动增量值

php - mysql中如何使用GROUP BY连接字符串

java - 创建另一个 res 子文件夹来放置布局文件

javascript - requireJS优化: "Uncaught TypeError: undefined is not a function"

java - spring-core 4.3.8 缺少 EmptyVisitor 类?