Hegemon
Hegemon

Reputation: 433

How to manipulate Strings in Scala while using the Play framework?

I am using the play framework 2.2.1 and I have a question concerning the manipulation of Strings within view templates. Unfortunately I am not very familiar with the Scala programming language nor its APIs. The strings are contained in a List which is passed from the controller to the view and then I use a loop to process each string before they are added to the html. I would like to know how to do the following: trim, toLowerCase and remove spaces. As an example, if I have "My string ", I would like to produce "mystring". More specifically I would actually like to produce "myString", however I'm sure I can figure that out if someone points me in the right direction. Thanks.

UPDATE:

Fiaz provided a great solution, building on his answer and just for interest sake I came up with the following solution using recursion. This example is of course making many assumptions about the input provided.

@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 ""
}

Upvotes: 0

Views: 2100

Answers (2)

Faiz
Faiz

Reputation: 16255

If you want to know how to do just the trimming, lower-casing and joining without spaces, try this perhaps?

// Given that s is your string
s.split(" ").map(_.toLowerCase).mkString

That splits a string into an array strings, splitting is done on one or more spaces so that gives you trimmed strings. You then map each element in the array with the function (x => x.toLowerCase) (for which the shorthand is (_.toLowerCase)) and then join the Array back into a single string using the mkString method that collections have.

So let's say you want to capitalize the first letter of the each of the space-split bits:

Scala provides a capitalize method on Strings, so you could use that:

s.split(" ").map(_.toLowerCase.capitalize).mkString

See http://www.scala-lang.org/api/current/scala/collection/immutable/StringOps.html

One suggestion as to how you can get the exact output (your example 'myString') you describe:

(s.split(" ").toList match { 
   case fst::rest => fst.toLowerCase :: rest.map(_.toLowerCase.capitalize)
   case Nil => Nil }
).mkString

Upvotes: 3

mikowiec
mikowiec

Reputation: 501

There is example of using the string manipulation below:

@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');">

This example obtained from https://github.com/joakim-ribier/play2-scala-gallery

Upvotes: 1

Related Questions