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


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"

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

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

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

现在a = "HelloWorld!!"的值

我创建了arrayList来修改字符串内容的数组,

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

//这将实现我的结果。

相关内容

  • 没有找到相关文章

最新更新