How to replace just first instance of the String using Regex Replace

I am trying to replace just the first instance of “ON” in this text file but all of it’s instance are getting replaced.

INPUT
Button1 ON
Button2 ON
Button3 ON

Expected OUTPUT
Button1 OFF
Button2 ON
Button3 ON

If its datatable, try by lookup and change only the 1st occurrence.

@Neeraj_Upadhyay
You can use the string methods Substring and IndexOf to accomplish this.

If your variable is called myString you can do
myString = myString.Substring(0,myString.IndexOf("ON")) + "OFF" + myString.Substring(myString.IndexOf("ON")+"ON".Length)

This takes the part of the string before your word, adds the replacement word, and then adds the the part of the string after your word.

It;s a text file and I have just provided a simple scenario in real it contains big sentences so i can’t just copy it in excel and change cell value

Thanks @DanielMitchell it’s working as expected now.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.