Results 1 to 15 of 15

Thread: [RESOLVED] Convert list of string to string

  1. #1

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Resolved [RESOLVED] Convert list of string to string

    Is there a better way to get a list of the elements in a List of String?
    vb.net Code:
    1. Clipboard.SetText(String.Join(vbCrLf, strNumbersLeftOver.ToArray))

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Convert list of string to string

    What do you intend to do with your list(of string)?

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,044

    Re: Convert list of string to string

    What would make a different alternative be rated as better to you?
    My usual boring signature: Nothing

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Convert list of string to string

    an improvement might be possible, but it really depends what you want to use it for...

  5. #5

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: Convert list of string to string

    I just was wondering if there was a method I was missing. In a StringBuilder it's just ToString. I was wondering if there was some equivalent I was missing. IE I have a pragmatic solution but I was wondering if there was a more elegant solution. I just want a list to use in my text editor, Excel, or whatever to deal with.

  6. #6

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: Convert list of string to string

    Oh, and .paul I did many things with the list but at last I just wanted the remnants to deal with a deficient process. This is a sort of failure audit. I wrote a program. There should be no left overs. These are the left overs that should not be. I then consider them and investigate what is deficient in my process.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Convert list of string to string

    Your string.join is efficient. As it is, you can paste it into a textbox or a range of excel cells...
    I'd be concerned that your program doesn't deal with any eventuality internally within the code though.

  8. #8

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: Convert list of string to string

    I feel like people too offense by my question. I don't understand why.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Convert list of string to string

    No one took offence to your question. We needed more information about the context you were using your code in so we could suggest an alternative, or agree it was ok. No offence taken.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Convert list of string to string

    Also, if you're targeting .NET 4.0 or later then there's no need to call ToArray. From .NET 4.0, String.Join takes an IEnumerable(Of String) or an IEnumerable(Of T) as well as a String array.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,044

    Re: Convert list of string to string

    I don't think anybody was offended, either. It's a pretty good question, nice and geeky, but "better" is in the eye of the beholder. Some folks love terse code, while others would accept more verbose for better performance....even if the difference would be hard to measure. It really is a matter of personal preference.

    Based on what you said, I'll mention that I was using a list(of String) to hold error messages in a certain project. Naturally, I'd want to be able to do a few things with whatever was in that list, including exporting the whole thing, showing it in a messagebox (nicely formatted), and so forth. Therefore, rather than just using a list, I wrapped that list in a class that added some different output/formatting methods. It wouldn't actually be quite as efficient as .Join for most of those methods, and it certainly wasn't more terse, but it was pretty convenient overall.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: Convert list of string to string

    Shaggy I'm glad no one was offended, as usual I have something that works but I want to know if its the best way. And you're right, "better" is a word that can be taken many ways. I should have said something like "Best practice" or "intended use". In many cases I've written a loop for something and found that the object had a built in method for it. For instance a long time ago I created an array of string from File.ReadAllLines and then made a loop to go though them all. Later I realized I could just put it in the For Each and avid creating an array. It was more elegant and simpler looking but I didn't appreciate any performance benefit. In this case I was thinking like with StringBuilder how there's a ToString option. But of course ToString doesn't work that way in a list of string. Or like a week ago when I discovered that a TextBox could be configured to take all letter keys to upper case. I wrote a whole routine for that was unnecessarily. Thanks for the idea on the class. I need to remember that they can be used for small things or to keep data too.

    Regarding everyone's idea of better is to; first use the classes and methods MS intend us to use for a certain task, IE use the right tool for the job and use the tool correctly; then to make my code easy to read and understandable, I'd rather take a slight performance hit that make something I can't read later; then use the fewest lines of code; and finally performance. And the last is easy to say because most of the things I write it's not important. how fast they are.

    Thanks John on the ToArray advice. I had never noticed that. Exactly the kind of improvement I was looking for.

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Convert list of string to string

    I just found the question ambiguous, which is why I figured the others were asking what you wanted to do with the list.
    "Is there a better way to get a list of the elements in a List of String?"
    I found the meaning of "list of the elements" confusing since I think of a List of String as a list of elements.

    If the question read:

    Is there a better way to create a delimited string from a List of String?

    then I would have understood you wanted to create a single string with delimiters.
    Of course your code does that, and I could probably have deduced the above question is what you were looking for if I made the assumption that what your code produced is what you wanted.

    In which case, the question also might have been:

    The following code produces the string I want, but is there a better method of producing this string from a List of String?

    Of course, that brings us back to the subjective opinion of what does "a better method" mean.
    A more objective question might have been:

    The following code produces the string I want, but is there an existing method or other preferred approach to producing this string from a List of String?

    I guess I've belabored this more than enough. I just didn't understand the question when I read it.

  14. #14

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: [RESOLVED] Convert list of string to string

    Thank you for the feedback. Part of the problem is how technology, legal, and others often re-purposes terms. A list to most of humanity is something you might take to the store to get groceries. But in this case a "List" is a very specific object. And I want a list in a string, imagine a grocery list in Notepad, but I'm trying to derive it from a "List (of String)". Ug. Linguist coincidence.

    The funny part is i'm often criticized for being long winded. But it seems when I try to be succinct I'm often confusing.

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,044

    Re: [RESOLVED] Convert list of string to string

    Yeah, I just love the fact that MS used the word "access" as the name of a database. That makes it great fun to talk about storing and retrieving data.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width