String 如何在使用Play框架时操作Scala中的字符串?
我正在使用play framework 2.2.1,我有一个关于视图模板中字符串操作的问题。不幸的是,我不太熟悉Scala编程语言及其API。字符串包含在一个列表中,该列表从控制器传递到视图,然后我使用一个循环来处理每个字符串,然后再将它们添加到html中。我想知道如何做到以下几点:修剪、缩小和删除空格。例如,如果我有“mystring”,我想生成“mystring”。更具体地说,我实际上想制作“myString”,但是我相信如果有人给我指出了正确的方向,我可以弄清楚这一点。谢谢 更新: Fiaz提供了一个很好的解决方案,基于他的答案,出于兴趣,我使用递归提出了以下解决方案。这个例子当然对所提供的输入做出了许多假设String 如何在使用Play框架时操作Scala中的字符串?,string,scala,playframework-2.2,String,Scala,Playframework 2.2,我正在使用play framework 2.2.1,我有一个关于视图模板中字符串操作的问题。不幸的是,我不太熟悉Scala编程语言及其API。字符串包含在一个列表中,该列表从控制器传递到视图,然后我使用一个循环来处理每个字符串,然后再将它们添加到html中。我想知道如何做到以下几点:修剪、缩小和删除空格。例如,如果我有“mystring”,我想生成“mystring”。更具体地说,我实际上想制作“myString”,但是我相信如果有人给我指出了正确的方向,我可以弄清楚这一点。谢谢 更新: Fia
@formatName(name: String) = @{
def inner(list: List[String], first: Boolean): String = {
if (!list.tail.isEmpty && first) list.head + inner(list.tail, false)
else if (!list.tail.isEmpty && !first) list.head.capitalize + inner(list.tail, false)
else if (list.tail.isEmpty && !first) list.head.capitalize
else list.head
}
if (!name.trim.isEmpty) inner(name.split(' ').map(_.toLowerCase).toList, true)
else ""
}
如果你想知道如何做修剪,降低外壳和连接没有空间,试试这个吧
// Given that s is your string
s.split(" ").map(_.toLowerCase).mkString
如果将字符串拆分为数组字符串,则会在一个或多个空格上进行拆分,从而生成修剪过的字符串。然后使用函数(x=>x.toLowerCase)
(其缩写为(uu.toLowerCase)
)映射数组中的每个元素,然后使用集合具有的mkString
方法将数组重新连接到单个字符串中
假设你们想将每个空间分割位的第一个字母大写:
Scala为字符串提供了一个大写
方法,因此您可以使用该方法:
s.split(" ").map(_.toLowerCase.capitalize).mkString
看
关于如何获得您描述的确切输出(您的示例“myString”)的一个建议:
(s.split(" ").toList match {
case fst::rest => fst.toLowerCase :: rest.map(_.toLowerCase.capitalize)
case Nil => Nil }
).mkString
如果你想知道如何做修剪,降低外壳和连接没有空间,试试这个吧
// Given that s is your string
s.split(" ").map(_.toLowerCase).mkString
如果将字符串拆分为数组字符串,则会在一个或多个空格上进行拆分,从而生成修剪过的字符串。然后使用函数(x=>x.toLowerCase)
(其缩写为(uu.toLowerCase)
)映射数组中的每个元素,然后使用集合具有的mkString
方法将数组重新连接到单个字符串中
假设你们想将每个空间分割位的第一个字母大写:
Scala为字符串提供了一个大写
方法,因此您可以使用该方法:
s.split(" ").map(_.toLowerCase.capitalize).mkString
看
关于如何获得您描述的确切输出(您的示例“myString”)的一个建议:
(s.split(" ").toList match {
case fst::rest => fst.toLowerCase :: rest.map(_.toLowerCase.capitalize)
case Nil => Nil }
).mkString
下面是使用字符串操作的示例:
@stringFormat(value: String) = @{
value.replace("'", "\\'")
}
@optionStringFormat(description: Option[String]) = @{
if (description.isDefined) {
description.get.replace("'", "\\'").replace("\n", "").replace("\r", "")
} else {
""
}
}
@for(photo <- photos) {
<div id="photo" class="random" onclick="fadeInPhoto(@photo.id, '@photo.filename', '@stringFormat(photo.title)', '@optionStringFormat(photo.description)', '@byTags');">
@stringFormat(值:String)=@{
值。替换(“'”,“\\”)
}
@optionStringFormat(描述:Option[String])=@{
如果(已定义说明){
description.get.replace(“'”,“\\”).replace(“\n”,”).replace(“\r”,”)
}否则{
""
}
}
@对于(photo,下面是使用字符串操作的示例:
@stringFormat(value: String) = @{
value.replace("'", "\\'")
}
@optionStringFormat(description: Option[String]) = @{
if (description.isDefined) {
description.get.replace("'", "\\'").replace("\n", "").replace("\r", "")
} else {
""
}
}
@for(photo <- photos) {
<div id="photo" class="random" onclick="fadeInPhoto(@photo.id, '@photo.filename', '@stringFormat(photo.title)', '@optionStringFormat(photo.description)', '@byTags');">
@stringFormat(值:String)=@{
值。替换(“'”,“\\”)
}
@optionStringFormat(描述:Option[String])=@{
如果(已定义说明){
description.get.replace(“'”,“\\”).replace(“\n”,”).replace(“\r”,”)
}否则{
""
}
}
@对于(照片足够简单,欣赏输入足够简单,欣赏输入