简体   繁体   中英

Split string in GSP grails

How to split a string and get first words?

studentController.groovy

def student = {
     def ex = new ArrayList() 
     ex[0]= "Steven | ABCDEF0123456" 
     ex[1]= "Steven | ABCDEF0123456" 
     //student's value

     [studentlist:ex] //send to gsp
}

student.gsp

<g:each in="${studentlist}" status="i" var="stdnt">
   <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
      <td>${stdnt}</td> //i want this show "Steven"
      <td>${stdnt}</td> // and this show ABCDEF0123456
   </tr>
</g:each>

How can I get the first word and second word?

You want Java's String.split() method:

List ex = "Steven | ABCDEF0123456".split('\\|')*.trim()

[studentlist:ex] //send to gsp

If you know there are only two items in the list, then you can reference them via studentlist[0] and studentlist[1] . It's a little unclear what you expect from the controller currently (a list of lists?):

<td>${studentlist[0]}</td>
<td>${studentlist[1]}</td>

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