![]() We will discuss some more Scala concepts in my coming posts.++ is binary operator(also called method) that is used to concatenate two collections.eg. That’s it all about “Scala’s slice function” usage. String’s character access returns Char where as substring & slice functions returns String as shown below. NOTE:- If we try to access String characters in out of range, we get StringIndexOutOfBoundsException as shown above. : String index out of range: -1Īt (String.java:658)Īt $.apply$extension(StringOps.scala:38) NOTE:- In Scala, We can access String characters just like an Array elements as shown below: Performance is almost similar and ignorable.Functionality wise and syntax wise there is no difference.Here both str.slice(6,9) and str.substring(6,9) are returning same value.ĭifference between slice and substring functions of String class String’s substring function works same as it’s slice function as shown below: Until-index = 9 (It’s exclusive so we need to consider till index = 8 only) How are you?Īs we know, String index starts with zero. That means “StringOps” is an implicit class of String class. When we use slice function on Java’s String objects, Scala Compiler internally converts this String object into StringOps class object to use this slice function. But this class does NOT have slice function. NOTE:- In Scala, we use Java’s String class. It defines slice function as shown below: In Scala API, “StringOps” class is defined in package. If we access an empty list, we will get empty list only as shown below Same as Array examples, we will get same results for any Collection API. For instance, List class defines this function as shown below:ĭef slice(from: Int, until: Int): List In Scala’s Standard API, most of the classes defines this slice function which supports index based elements access. ![]() NOTE:- Unlike Java, it does not throw any ArrayIndexOutOfBoundsException. If we give 2nd parameter value in beyond it’s available index as shown above (Available max index value in marksArray is 4 only as it’s length = 5), it just ignores that value and returns up-to available index only. If we give -ve values, it just starts with available index as shown above. It’s not possible to retrieve a set of elements from an array from 3 to 2 indices right. If we give same values for start and end like above we will get empty array why? It starts with 0 index that is first element and retrieves all elements until 2 means index = 1 that’s why we got 0th element and 1st element here. Int of Array with 5 values are created so it’s index start value is 0 and index end value is 4. Here ‘from’ is the starting index (Inclusive) of the Array and ‘until’ is the ending index (Exclusive) of the Array. In Scala API, Array class defines slice function as follows:ĭef slice(from: Int, until: Int): Array Here elements numbers for an Array of Numbers, Characters for a String, an object for a Collection. Slice function extract elements starting from ‘first-index’ (Inclusive) to ‘until-index’ (exclusive). It should be zero or any any positive integer less than the length of the Collection or String or Array. The “slice” function parameter’s usage are described in the following table: S.No. The following function is defined in Scala API’s Seq class.ĭef slice(from-index: Int, until-index: Int): Seq Here “ScalaAPIClass” means any Scala Collection class (which supports index based access like Seq, List etc), String, Array classes. In Scala Standard Library (API), this slice function is defined as follows:ĭef slice(from-index: Int, until-index: Int): ScalaAPIClass Here Bread.slice(0,10) means take bread slices from 0 to 9 so total 10 Bread slices.(Just to resemble Scala’s slice function here I’m using zero, but assume this as one.) ![]() We can use this slice function in our regular real-world life too as shown below. It takes two parameters of “Int” type and returns subset or whole or none element(s) of original Collection (or String or Array). In Scala API, ‘slice’ function is used to select an interval of elements. We will pick-up each API and discuss them in-depth with suitable examples in next section. Scala slice function is available in the following API classes: Today we will look into Scala slice function.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |