Scala String substring(int beginIndex, int endIndex) method with example

The substring(int beginIndex, int endIndex) method is utilized to find the sub-string from the stated String which starts and ends with the index specified.

Method Definition: String substring(int beginIndex, int endIndex)

Return Type: It returns string which is the part of the stated String.



Note: It is same as sub-sequence method but the only difference between the two is that sub-sequence returns CharSequence but the above method returns string.

Example: 1#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of substring()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Applying substring method
        val result = "GeeksforGeeks".substring(4, 8)
          
        // Displays output
        println(result)
      
    }

chevron_right


Output:

sfor

Example: 2#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of substring()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Applying substring method
        val result = "GeeksforGeeks".substring(0, 4)
          
        // Displays output
        println(result)
      
    }

chevron_right


Output:

Geek


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.