Shabeer Mothi
Shabeer Mothi

Reputation: 137

How to trim String in Java

I have a string:

Am trying to do the below :

int i =0;

for(String s : appFields){
    i++;
    String divid = "chart_"+i;
    divid = divid.replaceAll("[\\r\\n]+$", "");
}

I would like to trim it so that the value is only chart_1 and so on.

Can someone help me please?

<%
    String[] appFields = "Account Information,Action Status,Activity Name,Activity Status,Last Activity Timestamp,Geographical Region,Enterprise Status,Business Process,Numer of Pages,Message Direction".split(",");

    int i =0;

    for(String s : appFields){
        i++;
        String divid = "chart_"+i;
        divid = divid.replaceAll("[\\r\\n]+$", "");
%>
<tr>
    <td><% out.println(i); %></td>
    <td><% out.println(s); %></td>
    <td class='with-3d-shadow with-transitions'><p><svg id="<% out.println(divid); %>" class="sparkline"></svg></p></td>
</tr>
<% 
    }
%>

This is the output i get in Chrome Web Inspector

<td class="with-3d-shadow with-transitions"><p><svg id="chart_1
" class="sparkline"></svg></p></td>

Upvotes: 1

Views: 196

Answers (2)

evanchooly
evanchooly

Reputation: 6243

temp = temp.trim();

Strings are immutable so operations return new Strings that you have to assign back to the original reference variable.

Upvotes: 6

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280170

Change your line to

<svg id="<%out.print(divid);%> " class="sparkline"></svg>

In other words, use print instead of println. println adds a new line after your String.

Upvotes: 1

Related Questions