1
votes

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?

1

1 Answers

1
votes

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>