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:
[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.
CodePudding user response:
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/>");