Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using c#, why are the letters r and b being interpreted as HTML breaks in label text and how can I stop it?

I have some code that takes address text from a label and attempts to split that string by HTML line breaks to get each address line in a string array:

string[] straAddressLines = lbBillStreetAddress.Text.Split("<br/>".ToCharArray());

When the label contains a lower case r or b, the code is interpreting those characters as HTML breaks. So for example, if the text is "1 Albert Street", the resulting array is:

[0]: 1 Al
[1]: e
[2]: t St
[3]: eet

Since there are no HTML breaks in the string, I would expect it to be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[0]: 1 Albert Street 

Can anyone tell me what is happening here and how I can stop that behaviour? I’ve tried HTML and URL encoding/decoding but no difference.

>Solution :

It is because you are passing a char array for Split method, so it splits the string by each character. Since you want to split by <br/> you need to pass it as a string, instead of char[]:

lbBillStreetAddress.Text.Split(new [] { "<br/>" }, StringSplitOptions.None);

If you are using .NET 5+ (or .NET Core 2+) there is a more convenient overload that just takes a string as separator:

lbBillStreetAddress.Text.Split("<br/>");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading