How to remove N characters from the middle of a string in Scala

1 Answer

0 votes
object Main extends App {
  var str = "abc123def"
  
  val mid = Math.floor(str.length / 2).toInt
  val N = 3
  
  str = str.substring(0, mid - Math.floor(N / 2).toInt) + 
        str.substring(mid + 1 + Math.floor(N / 2).toInt)
  
  println(str)
}


   
/*
           
run:
     
abcdef
       
*/

 



answered Sep 11, 2024 by avibootz
...