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

The command Split not working from a List<string> but from a normal string

I am a bit puzzled about the results of these tests and the fact that in one of the tests .Split(' ').Last() not working.

I have the following:

print("1) " + "Lidingö kommun Sverige".Split(' ').Last());

string xx = "Lidingö kommun Sverige";

print("2) " + xx.Split(' ').Last());

print("3) " + Utility.setup_List[0].freqFullLocationName);

print("4) " + Utility.setup_List[0].freqFullLocationName.Split(' ').Last());

The Utility.setup_List[0].freqFullLocationName is a string.

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

I get the following results:

1) Sverige
2) Sverige
3) Lidingö kommun Sverige 
4) 

Given that the Utility.setup_List[0].freqFullLocationName is a string I do not understand why it is not split correct. I have tested to first copy to a new string and then split with the same result.

>Solution :

The content of freqFullLocationName is "Lidingö kommun Sverige " with a space at the end.

So, Split() works as expected because it it recognizes the space and splits "Sverige" from an empty string entry after the following space.

If you want to fix this behavior, you can specify the StringSplitOptions.RemoveEmptyEntries option when calling Split(). Or use Trim() on the string before calling Split().

print("4) " + Utility.setup_List[0].freqFullLocationName.
    Split(' ', StringSplitOptions.RemoveEmptyEntries).Last());
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