如何限制使用Scala显示的字符串中的字符数 [英] How to limit the number of characters in a string displayed using Scala

查看:128
本文介绍了如何限制使用Scala显示的字符串中的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Scala很新,并且不太了解很多语法,所以我正在寻找一些指导。我正在将ArrayList读入网页并以表格格式显示信息。以下是我页面中html的示例:

I'm fairly new to Scala and don't know a lot of the syntax, so I'm looking for a little guidance. I'm reading in an ArrayList to a web page and displaying the information in a table format. Here is an example of the html in my page:

<table>
  <thead>
    <tr>...</tr>
  </thead>
  <tbody>
    @for(info <- Info.all) {
      <tr>
          <td>@info.name</td>
          <td>@info.id</td>
          <td>@info.birthdate</td>
          <td>@info.notes</td>
      </tr>
    }
  </tbody>
</table>

我想要完成的是在第一个之后切断注释列字段的12个字符('notes'是字符串类型)。换句话说,如果ArrayList的notes字段中有超过12个字符,则它只需显示前12个字符并切断其余字符。有没有办法使用Scala这样做?如果没有,那我该如何处理这个问题?

What I'm trying to accomplish is for the "notes" column to be cut off after the first 12 characters of the field ('notes' is a String type). In other words, if there is more than 12 characters in the "notes" field of the ArrayList, then it needs to only display the first 12 characters and cut off the rest. Is there a way of doing this by using Scala? If not, then how should I approach this problem?

感谢您的帮助!

推荐答案

这个怎么样?

@info.notes.take(12)

或者像这样的java风格:

Or java-style like this:

@info.notes.substring(0, 12)

这篇关于如何限制使用Scala显示的字符串中的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆