Just a quick note here today on how to show methods from the
String class in the Scala REPL, as well as methods from the
StringOps class, which aren't seen as easily.
First, if you've used tab completion in the REPL, you may already know that you can show many Scala
String class methods in the REPL. If you hit the [Tab] key once you'll see a short list of
scala> "foo".[Tab] + asInstanceOf charAt codePointAt codePointBefore codePointCount compareTo compareToIgnoreCase concat contains contentEquals endsWith equalsIgnoreCase getBytes getChars indexOf intern isEmpty isInstanceOf lastIndexOf length matches offsetByCodePoints regionMatches replace replaceAll replaceFirst split startsWith subSequence substring toCharArray toLowerCase toString toUpperCase trim
And then if you hit the [Tab] key a second time you'll see even more
String class methods:
scala> "foo".[Tab][Tab] != ## $asInstanceOf $isInstanceOf + == asInstanceOf charAt codePointAt codePointBefore codePointCount compareTo compareToIgnoreCase concat contains contentEquals endsWith eq equals equalsIgnoreCase getBytes getChars getClass hashCode indexOf intern isEmpty isInstanceOf lastIndexOf length matches ne notify notifyAll offsetByCodePoints regionMatches replace replaceAll replaceFirst split startsWith subSequence substring synchronized this toCharArray toLowerCase toString toUpperCase trim wait
How to show StringOps methods in the REPL
You may be aware that the Scala
String class appears to have many more methods, and these methods are made available to you through some implicit conversion magic. Those methods are actually in the StringOps class, and at the moment the only way I know to show those methods is like this:
scala> val s = new scala.collection.immutable.StringOps("s") s: scala.collection.immutable.StringOps = s scala> s.[Tab] * ++ ++: +: /: /:\ :+ :\ > >= addString aggregate apply asInstanceOf canEqual capitalize collect collectFirst combinations compare compareTo contains containsSlice copyToArray copyToBuffer corresponds count diff distinct drop dropRight dropWhile elements endsWith equalsWith exists filter filterNot find findIndexOf findLastIndexOf first firstOption flatMap fold foldLeft foldRight forall foreach format formatLocal groupBy grouped hasDefiniteSize head headOption indexOf indexOfSlice indexWhere indices init inits intersect isDefinedAt isEmpty isInstanceOf isTraversableAgain iterator last lastIndexOf lastIndexOfSlice lastIndexWhere lastOption length lengthCompare lines linesIterator linesWithSeparators map max maxBy min minBy mkString nonEmpty padTo par partition patch permutations prefixLength product projection r reduce reduceLeft reduceLeftOption reduceOption reduceRight reduceRightOption replaceAllLiterally repr reverse reverseIterator reverseMap reversedElements sameElements scan scanLeft scanRight segmentLength seq size slice sliding sortBy sortWith sorted span split splitAt startsWith stringPrefix stripLineEnd stripMargin stripPrefix stripSuffix sum tail tails take takeRight takeWhile toArray toBoolean toBuffer toByte toDouble toFloat toIndexedSeq toInt toIterable toIterator toList toLong toMap toSeq toSet toShort toStream toString toTraversable union updated view withFilter zip zipAll zipWithIndex
There are several drawbacks here, including the part where you have to remember what package the
StringOps class is in so you can import it or invoke it as shown.
If you know of a better way to list these
StringOps methods, I'd be very glad to hear about it. Just leave a note in the Comments section below.
This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.