Match and remove first linebreak from string

Hello,

I have a string containing a lot of text, spaces and line breaks. The problem i am facing is that i only wanna match the first line break and remove it.

I can match all the line breaks and remove them, but cannot figure out how to match only the first one.

Any help would be appreciated.

Hi @Ninjabullen ,

Would you be so kind as to provide us with some sample text to work with?
I can create the pattern for you using regex.

Kind Regards,
Ashwin A.K

Absolutely, forgott to mention that we would not know the last word/letter on the first line or the first word in the second line.

For example:
"
This is a test random word
random word as well And this line break should be removed
But not this one
Or this one
or this one as well.
"

So i would like the first line to contain, but nothing else changes:
This is a test random word random word as well and this line break should be removed

Hi @Ninjabullen,

is this the desired output?

image

If so, then this is the pattern that is used here →

/r?/n
System.Text.RegularExpressions.Regex.Replace(str_txtData,"\r?\n"," ")

RemoveFirstLineBreak.xaml (5.3 KB)

Kind Regards,
Ashwin A.K

Seems like it removed all the line breaks.

Desired output would be:
This is a test random word random word as well and this line break should be removed
But not this one
Or this one
or this one as well.

Hi there @Ninjabullen,
I trust you are having a fantastic day!

You should be able to achieve this via Strings.Replace:
Strings.Replace(String, String, String, Int32, Int32, CompareMethod) Method (Microsoft.VisualBasic) | Microsoft Docs

You can configure it as:
YourString= Strings.Replace(YourString, VBCRLF, String.Empty, 1, 1)

This will ultimately say, replace the first instance of VBCRLF in the String value.

Example below:

Please let me know if you have any questions.
Thanks once again for your continued support,
Josh

2 Likes

Thank you Mr_JDavey, this was exactly what i was after.

Worked fine.

Wish you a fantastic day as well. :slight_smile:

1 Like

Seems like I was a little too late.

Fantastic solutions by @Mr_JDavey though!

I’ll just leave what I’ve found here in case you are interested:

image

System.Text.RegularExpressions.Regex.Replace(str_txtData,"(?<=^.*)\r?\n"," ")

Kind Regards,
Ashwin A.K

2 Likes

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