Scala Programming: Get the character at the given index within the String and print the length of the string
Scala Programming String Exercise-1 with Solution
Write a Scala program to get the character at the given index within a given String. Also print the length of the string.
Sample Solution:
Scala Code:
object Scala_String {
def main(args: Array[String]): Unit = {
var str = "Scala Exercises!";
println("Original String = " + str);
// Get the character at positions 0 and 10.
var index1 = str.charAt(0);
var index2 = str.charAt(10);
var index3 = str.charAt(15);
// Print out the results
println(s"The character at position 0 is ${index1}");
println(s"The character at position 10 is ${index2}");
println(s"The character at position 15 is ${index3}");
println(s"Length of the string: ${str.length}")
}
}
Sample Output:
Original String = Scala Exercises! The character at position 0 is S The character at position 10 is c The character at position 15 is ! Length of the string: 16
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Scala Programming String Exercises Home.
Next: Write a Scala program to get the character (Unicode code point) at the given index within the String.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/scala-exercises/string/scala-string-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics