An approach in which you can change the symbol values for each position, in this case those before position 12 ,
creditCardNo.zipWithIndex.map (c => if (c._2 < 12) '*' else c._1 ).mkString
Please note that despite the ability to change each position separately, this is not the most efficient way to manipulate strings.
source share