Is it possible to do:
"hello, I have 65 dollars".replaceFirst("65", "$")
Current result is
scala> "hello, I have 65 dollars".replaceFirst("dollars", "$")
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
....
The expected result in scala 2.10:
hello, I have 65 $
Problem is with symbol $
, I need to process it as string not regexp. I tried to put it into """
, or raw""
but nothing helped
First, you have to escape dollar character because right now it is treated as a part of regex (end-of-the-string sign):
It is much more likely you wanted to replace "dollars" word:
You can either double escape the dollar sign:
Or use Scala triple quotes and single escape.
Either way to you need end up with a String literal equal to "\$" to escape the dollar with a backslash.
EDIT
I'm not sure that you want "65 $" - isn't "$65" a better format? For this you need a capture group and a backreference
Output: