How to convert part of a string between two indexes to uppercase in Scala

1 Answer

0 votes
object ConvertPartOfStringToUppercase_Scala {
  def convertPartOfStringToUppercase(str: String, start: Int, end: Int): String = {
    // Extract the part of the string before the start index
    val before = str.substring(0, start)
    
    val upperPart = str.substring(start, end).toUpperCase
    
    // Extract the part of the string after the end index
    val after = str.substring(end)
    
    before + upperPart + after
  }

  def main(args: Array[String]): Unit = {
    var s = "scala programming"
    
    s = convertPartOfStringToUppercase(s, 2, 5)
    println(s)
    
    s = convertPartOfStringToUppercase(s, 13, 14)
    println(s)
  }
}




/*
run:

scALA programming
scALA programMing

*/

 



answered Sep 30, 2024 by avibootz
...