We help IT Professionals succeed at work.

VB.net Replace first longer then shorter strings

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

I have a long string in which I first need to replace longer words and then single chatacter.
At the moment I enclose the replaced parts in square brackets so wondered if there was a way to avoid the replaced parts:

For example

Replace("code 1 will be used on platform C" ,"code 1","[C1235])
this results in "[C1235] will be used on platform C"

now if I want to Replace("[C1235] will be usedon Platform C" ,"C","D")

how do I ignore the part enclosed in brackets to get  "[C1235] will be usedon Platform D"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Project manager
Commented:
The easiest way:

Replace("[C1235] will be usedon Platform C" ,"Platform C","Platform D")


The harder solution:
Dim myString As String = "[C1235] will be usedon Platform C"
myString.Substring(0, myString.IndexOf("]")+1)  + myString.Substring(myString.IndexOf("]") + 1).Replace("C", "D")
Most Valuable Expert 2012
Top Expert 2014
Commented:
If the single character that you are replace is only character in the word then you can try

Replace("[C1235] will be usedon Platform C" ," C"," D")
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
thanks