# how to replace the specifc part of one string to the specific part of other string

Hello ,

I have two strings-
1-PFC - PQTG P04 2019 20402-000-9200 Quebec Income Tax (RL-1)
2-PBC-US P05 2018 9131.030.20029008 ACCRUED UNION GROUP INS

Here I have to replace the P04 2019 part of string 1 to the P05 2018 part of string 2.
length of string1 and string 2 is not defined and P04 2018 part can come at any place of the string

Kindly try with this buddy
in_text1 = “1-PFC - PQTG P04 2019 20402-000-9200 Quebec Income Tax (RL-1)”
in_text2 = “2-PBC-US P05 2018 9131.030.20029008 ACCRUED UNION GROUP INS”

in_cond1 = in_text1.SubString(in_text1.ToString.Indexof(“P04”),8)
in_cond2 = in_text2.SubString(in_text2.ToString.Indexof(“P05”),8)

out_value = in_text1.ToString.Replace(in_cond1,in_cond2)

Cheers @Karan28

1 Like

@Karan28

Try this:

System.Text.RegularExpressions.RegEx.Replace(“P\d{2} \d{4}”,"")

1 Like

Hello Lakshman,

Please explain this because as per my understanding you are replacing this with the null value., is it?

1 Like

Hello Palaniyappan , thanks for replying
Actually I need to replace the string will be like P01 2018 , P012 2018, P03 2019, P10 218 , P11 2019 P12 2018

@Karan28

Because you didn’t specify anything and I thought you want to replace with null value. So, I wrote expression like that. If you want to replace with particular text then you can change it.

YourString.Replace(“old string”,“new String”)

thankyou your logic worked , but when the string to be replaced is P11 2018 , it will fail right?
for now I have used this logic- in_text1.SubString(in_text1.ToString.Indexof(“P0”),8) which is working for P01 2018 etc , but when the string to be replaced is P11 2018 it will fail right?

do you have a solution for this?

1 Like

Okay Lakshman , thankyou for replying

1 Like