YouTip LogoYouTip

Scala Pattern Matching

Scala Pattern Matching | Tutorial

Tutorial -- Learning not just technology, but dreams!

Scala Tutorial

Scala TutorialScala IntroductionScala Installation and Environment ConfigurationScala Basic SyntaxScala Data TypesScala Literals Scala Escape Characters Scala VariablesScala Access ModifiersScala OperatorsScala IF...ELSE StatementsScala LoopsScala Methods and FunctionsScala ClosuresScala StringsScala ArraysScala CollectionScala IteratorScala Classes and ObjectsScala TraitScala Pattern MatchingScala Regular ExpressionsScala Exception HandlingScala ExtractorScala File I/O

Scala Trait

Scala Regular Expressions

Scala Pattern Matching

Scala provides a powerful pattern matching mechanism, which is widely used.

A pattern match contains a series of alternatives, each starting with the keyword case. Each alternative contains a pattern and one or more expressions. The arrow symbol => separates the pattern from the expressions.

The following is a simple integer value pattern matching example:

Example

object Test {

def main(args: Array){

 println(matchTest(3))

}

def matchTest(x: Int): String = x match{

case 1=>"one"

case 2=>"two"

case _=>"many"

}

}

Execute the

← Html UrlencodeScala Traits β†’