要替换字符串数组中的空白

时间:2018-07-26 10:18:21

标签: java string-utils

<div id="<?php print $forklift_id; ?>" class="forklift_broken_modal">
                            <div id="modal_info_wrapper" class="modal_info_wrapper">
                                <h1 class="forklift_number_h" id="forklift_number_b"><?php print $forklift_id; ?></h1>
                                <h2 class="forklift_name_h" id="forklift_name_b"></h2>
                                <div class="forklift_info_box">
                                    <p class="forklift_info_p" id="forklift_info_b"></p>
                                </div>  
                                <h2 class="forklift_name_h">charging spot</h2>
                                <p class="forklift_info_p" id="forklift_chargin_b"></p>
                            </div>
                            <form action="">
                                <div class="modal_input_wrapper">
                                    <input title="this information will be sent to management" class="radio_btn" type="radio" name="broken" value="broken">broken
                                    <input class="broken_info" type="text" name="broken_info" placeholder="write short information how its broken here."><br>
                                    <input class="radio_btn" type="radio" name="broken" value="not_broken">intact<br>
                                </div>
                                <div class="modal_footer">
                                    <input title="this information will be sent to management" class="input_submit" value="save" name="save_broken_details" type="submit">
                                    <button onclick="close_all_modals();" class="input_submit">exit</button>
                                </div>
                            </form>
                        </div>

结果:

private static final String SQL_INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})";
private static final String SQL_CREATE = "CREATE TABLE ${table}(${keys} VARCHAR(20) NOT NULL)";
private static final String TABLE_REGEX = "\\$\\{table\\}";
private static final String KEYS_REGEX = "\\$\\{keys\\}";
private static final String VALUES_REGEX = "\\$\\{values\\}";

String[] headerRow = csvReader.readNext();//getting(Machine Name , Value)
String[] stripedHeaderRow = StringUtils.stripAll(headerRow);

String query2 = SQL_CREATE.replaceFirst(TABLE_REGEX, tableName);
query2 = query2.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, " VARCHAR(20) NOT NULL,"));

预期结果:

"Machine Name, Value"

3 个答案:

答案 0 :(得分:0)

您可以在JAVA中使用正则表达式来实现。 例如:

String a = "Hello    World!!";
 a = a.replaceAll("\\s","");

现在值为a = "HelloWorld!!"

答案 1 :(得分:0)

假设您的数组位于temp变量中,

这是在每个字符串之间修剪所有空格的方法。

String[] temp = {"My Name Is", "And so Far", "All good", "Really?", 
                             "That makes no sense to me", "Oh! Somehthing Like This"};
Stream<String> stream = Arrays.stream(temp);
temp = stream.map(str -> str.replaceAll("\\s",""))
         .toArray(size -> new String[size]);

现在如果打印出来,它会给出这个。.

for(String st : temp)
        System.out.println(st);

输出:-

MyNameIs
AndsoFar
Allgood
Really?
Thatmakesnosensetome
Oh!SomehthingLikeThis

答案 2 :(得分:0)

我创建了arrayList来修改String内容的数组,

List<String> al = new ArrayList<String>();
for (String element: headerRow) 
     { 
         al.add(element.replace(' ', '_')); 

    }
String[] stripedHeaderRow = al.toArray(new String[al.size()]);//Creating new array of String

//这将满足我的要求。