0
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,"));

Result:

"Machine Name, Value"

Expected result:

"MachineName , Value"
  • Do you expect `stripAll()` to remove those spaces? The [documentation](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#stripAll-java.lang.String...-) clearly states "Strips whitespace from the start and end of every String in an array." – André Stannek Jul 26 '18 at 10:28

3 Answers3

0

You can do it with regular expression in JAVA. for example:

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

Now the value of a = "HelloWorld!!"

Gourango Sutradhar
  • 1,461
  • 10
  • 16
0

Assuming your array is in temp variable,

Here is how you can get all spaces trimmed between every string.

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]);

Now if printing it, it would give this..

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

Output:-

MyNameIs
AndsoFar
Allgood
Really?
Thatmakesnosensetome
Oh!SomehthingLikeThis
miiiii
  • 1,580
  • 1
  • 16
  • 29
0

I created arrayList to modify the array of String contents,

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

//This will fulfill my result.