简体   繁体   中英

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>
temp = temp.trim();

String是不可变的,因此操作将返回新的String ,您必须将其分配回原始引用变量。

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM