I have a string like :
string = abcdefghabcd
Now lets say I want to replace the first occurrence of a. I tried something like this :
string[string.indexOf('a')] = '0'
But this doesn’t seems to be working. Any other way I can do this?
Thanks in advance.
In Java you can use String.replaceFirst() :
Output will be :
Warning : the
replaceFirst()method takes a regex : so if you want to replace a special character like[you need to escape by putting a\before it.\being a special character itself, you need to double it as follow :Here is the documentation on Java Regular Expressions. Also, here is Oracle’s Java tutorial on manipulating Characters in Strings.