the blog for developers

Scala Goodness: RichString

Scala is a marvelous beast. Fire up the Scala shell and enter:

scala> "capitalize"
res0: java.lang.String = capitalize

scala> "capitalize".capitalize
res1: String = Capitalize

How can that be? "capitalize" is of type java.lang.String, and the Java class does not have a capitalize method. But the Scala RichString methods has (Scala 2.8 will use StringOps). And there are more methods to RichString, like reverse, drop, toDouble, toInt and toLong

scala> "123".toInt
res2: Int = 123

scala> "123".toLong
res3: Long = 123

Another nice one, is format. Not as nice as as GStrings in Groovy, but nevertheless:

scala> "hello %s".format("stephan")
res6: String = hello stephan

How can Scala do this? There is a conversion going on in Scala, which automatically converts your Java String to RichString when the method cannot be found in String but in RichString. The relevant code is in Predef (look for yourself and see how deep the rabbit-hole goes):

implicit def stringWrapper(x: String) = new runtime.RichString(x)

Scala Glory!

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.

2 Tweets

Leave a reply.

Comments

Scala has GStrings! Well, almost. Here we go:

scala> val n = “name”
n: java.lang.String = name

scala> <s>Hello { n }!</s>.text
res0: String = Hello name!

scala>

@Erik: Agreed, when the content is valid XML one can use XML literals for “GStrings”, would be nice to have real ones though, not everything I do is HTML ;-)

Stephan, any string works. It does /not/ have to be valid XML:

scala> val n = “<[[CDATA My name is invalid XML & <a &”
n: java.lang.String = My name is invalid XML & <s>Hello { n }!<s>.text
res1: String = Hello <[[CDATA My name is invalid XML & <a &!

scala>

Stephan, any string works. It does /not/ have to be valid XML:

scala> val n = “<[[CDATA My name is invalid XML & <s>Hello { n }!</s>.text
res3: String = Hello <[[CDATA My name is invalid XML & <a &!

Stephan, any string works. It does /not/ have to be valid XML:

scala> val n = “<[[CDATA My name is invalid XML & <a &”
n: java.lang.String = <[[CDATA My name is invalid XML & <a &

scala> <s>Hello { n }!</s>.text
res3: String = Hello <[[CDATA My name is invalid XML & <a &!

scala>

(This is my last attempt to find out the right escaping.)

Hmm. “any String works” => “any String works which escapes everything with a CDATA command to make it XML” ?

Would be like GStrings if this would work:

scala> val n = “stephan”
n: java.lang.String = stephan

scala> val s = “hello {n}”
s: java.lang.String = hello {n}

(But nice to know the CDATA trick, although it is ugly it might come in handy sometimes)

Stephan, no no. The “&lt[[CDATA" is completely non-valid XML (valid XML requires a closing "]]>”), it is just a string. I put it in there to emphasize that it is not valid XML.

Tyro

Erik – that trick is kind of neat. Right now it feels too “clever” but I’ll try it out on a couple friends and see what they think. If it flies, it comes close to giving me a real GString which would be wonderful.

@Erik: Could you paste to pastebin or something? Would really learn this, I’m to stupid to follow you :-)

Peter

Thanks Erik. So the only overhead is a leading “” and a trailing “.text”. I can live with that.

scala> val n = “stephan”
n: java.lang.String = stephan

scala> val s = hello {n}.text
s: String = hello stephan

scala> println(s)
hello stephan

I still don’t get it :-)

Leave a Reply

What people wrote somewhere else:

Published a short: “Scala Goodness: RichString” http://is.gd/6EP9c #scala

This comment was originally posted on Twitter

Nice stuff.. RT @codemonkeyism Published a short: “Scala Goodness: RichString” http://is.gd/6EP9c #scala

This comment was originally posted on Twitter

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?