想要从 String 数组中替换 WhiteSpace

Want to replace WhiteSpace from array of String

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"

预期结果:

"MachineName , Value"

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

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

现在的价值a = "HelloWorld!!"

假设您的数组在 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

我创建了 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

//这将满足我的结果。