×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala String charAt() Method

By IncludeHelp Last updated : October 20, 2024

Description and Usage

The charAt() method is used to get the character from the string at a given index.

Syntax

string_Name.charAt(index)

Parameters

The method accepts a single parameter which is the index of the character to be found.

Return Value

It returns a character which is found at the given index.

Example 1: Program to illustrate the working of charAt() method

object myObject {
    def main(args: Array[String]) {
        val myString = "scala"
    
        println("The string is '" + myString + "'")
    
        println("Character at index 4 is " + myString.charAt(4))
    }
}

Output

The string is 'scala'
Character at index 4 is a

Example 2: Program to print characters of string

object myObject {
    def main(args: Array[String]) {
        val myString = "scala"
        println("The string is '" + myString + "'")

        val stringLen = myString.length()
        println("Printing the string character by character :")

        for(i <- 0 to (stringLen - 1) )
            println(myString.charAt(i))
    }
}

Output

The string is 'scala'
Printing the string character by character :
s
c
a
l
a

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.