Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/library/scala/collection/StringOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1201,14 +1201,16 @@ final class StringOps(private val s: String) extends AnyVal {
def withFilter(p: Char => Boolean): StringOps.WithFilter = new StringOps.WithFilter(p, s)

/** The rest of the string without its first char.
* @note $unicodeunaware
*/
def tail: String = slice(1, s.length)
* @throws UnsupportedOperationException if the string is empty.
* @note $unicodeunaware
*/
def tail: String = if(s.isEmpty) throw new UnsupportedOperationException("tail of empty String") else slice(1, s.length)

/** The initial part of the string without its last char.
* @note $unicodeunaware
*/
def init: String = slice(0, s.length-1)
* @throws UnsupportedOperationException if the string is empty.
* @note $unicodeunaware
*/
def init: String = if(s.isEmpty) throw new UnsupportedOperationException("init of empty String") else slice(0, s.length-1)

/** A string containing the first `n` chars of this string.
* @note $unicodeunaware
Expand Down
14 changes: 14 additions & 0 deletions test/junit/scala/collection/StringOpsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,18 @@ class StringOpsTest {
assertEquals("de", "abcdef".collect { case c @ ('b' | 'c') => (c+2).toChar })
assertEquals(Seq('d'.toInt, 'e'.toInt), "abcdef".collect { case c @ ('b' | 'c') => (c+2).toInt })
}

@Test def init(): Unit = {
assertEquals("ab", "abc".init)
assertEquals("a", "ab".init)
assertEquals("", "a".init)
assertThrows[UnsupportedOperationException]("".init)
}

@Test def tail(): Unit = {
assertEquals("bc", "abc".tail)
assertEquals("b", "ab".tail)
assertEquals("", "a".tail)
assertThrows[UnsupportedOperationException]("".tail)
}
}