Liyakat Shaikh
Liyakat Shaikh

Reputation: 115

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"

Upvotes: 0

Views: 1396

Answers (3)

Liyakat Shaikh
Liyakat Shaikh

Reputation: 115

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.

Upvotes: 0

miiiii
miiiii

Reputation: 1610

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

Upvotes: 0

Gourango Sutradhar
Gourango Sutradhar

Reputation: 1619

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!!"

Upvotes: 0

Related Questions