Search code examples
stringjuliauppercase

How do I make a specific letter of a string uppercase in Julia?


How do I make a specific letter of a string uppercase, and not changing any of the other letters?

My example:

"this works" -> "this woRks" //Change made to letter 7
"this works" -> "this wOrks" //Change made to letter 6
"this works" -> "This works" //Change made to letter 1

My system uses characters with a UTF-8 encoding, so it needs to support uppercase for UTF-8 characters and not just ascii.


Solution

  • Unoptimized one-liner :)

    julia> s = "this is a lowercase string"
    "this is a lowercase string"
    
    julia> String([i == 4 ? uppercase(c) : c for (i, c) in enumerate(s)])
    "thiS is a lowercase string"