javastring-utils

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

Result:

"Machine Name, Value"

Expected result:

"MachineName , Value"

Solution

  • 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