Replace character from string with - , except on certain positions

Hi how do i replace characters of random strings to “-”, exception character first 2 and last 2 char?

e.g. ui------fd
ai---------ma
12------------91

thanks

Are you looking for this?
grafik

YourStringVar.Insert(2,"---")

Hi,

How about the following expression?

System.Text.RegularExpressions.Regex.Replace(yourString,"(?<=^..).{3}","---")

Regards,

Hi,

Your question have been changed…

Give the following try.

System.Text.RegularExpressions.Regex.Replace(yourString,"(?<=^..).*(?=..$)",New String("-"c,yourString.Length-4))

Regards,

Hi, oh sry i realised, its not as what i intended.
but how about in the future iff needed to change to as-------asd instead? Do i replace 4 with 5?

better you give a clear requirment statement

  • what is input string
  • what is output string

Hi,

(?<=^..) means 2 characters at the beginning of the string
(?=..$) means 2 characters at the end of the string

So if you want to remain last 3 characters, the following will work

System.Text.RegularExpressions.Regex.Replace(yourString,"(?<=^..).*(?=...$)",New String("-"c,yourString.Length-5))

Regards,

1 Like

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