Remove First Character from each line in Text line if Condition met

Dears,

How to Remove First Character from each line in Text file if Condition met ( Line start with hyphen “-”). Thanks

Hi,

There are some ways to achieve it. One of them is regex as the following, for example. Can you try this?

System.Text.RegularExpressions.Regex.Replace(strData,"^-","",System.Text.RegularExpressions.RegexOptions.Multiline)

Regards,

1 Like

Thanks @Yoichi , one thing , sometimes I have space prior to hyphen, so there is 2 cases : “Hyphen” and “Space+Hyphen”
“-Word”
" -Word"

Thanks in advance

Hi,

Do you mean if there is space+hyphen at the beginning of line, remove not first character but both characters? If so, the following will work.

    System.Text.RegularExpressions.Regex.Replace(strData,"^\s*-","",System.Text.RegularExpressions.RegexOptions.Multiline)

Regards,

1 Like

It works fine for those 2 cases, Sorry to bother you again , I forgot the 3rd Possibility :
Case 01 : Hyphen
Case 02: Space
Case 03: Hyphen+Space

Hi,

Do you wan to remove all whitespace(s) and hyphen(s) at the beginning of line? The following will work so.

System.Text.RegularExpressions.Regex.Replace(strData,"^[-\s]*","",System.Text.RegularExpressions.RegexOptions.Multiline)

Regards,

1 Like

Excellent!!! @Yoichi :+1:

1 Like

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