Results 1 to 8 of 8

Thread: [RESOLVED] Insert at a line

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Resolved [RESOLVED] Insert at a line

    If I have a string that has many lines in it, is it possible to insert a string at a certain line? I see that there is an insert function for strings. Is there a function to insert a string at a certain line of another string?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Insert at a line

    You'd have to know exactly where the beginning of the line you wanted to insert before was, which is not that easy. The simplest way is to Split the String on the line breaks, create a List(Of String) from the resultant array, Insert into that List, then use String.Join to join the list items back into a single String.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: Insert at a line

    I know that you can split a string into an array. Is there an inbuilt way to split a string into a list of string?

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Insert at a line

    Given that you're also trying to get a certain line of a string as well, it occurs to me that you probably shouldn't be storing it as a single string, but as a list of strings anyway. When you get the string initially, convert it to a List(Of String), and keep it as such whilst you're doing all these manipulations (get/set/insert etc) until you need to display it or write it to file or whatever, and only then convert it back to a single string.

    Even better would be to define a class to represent whatever this string ultimately is that exposes the Lines and a method for getting the full string out.

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Insert at a line

    Quote Originally Posted by Simon Canning View Post
    I know that you can split a string into an array. Is there an inbuilt way to split a string into a list of string?
    There is an inbuilt way of converting a String array to a List(Of String) (it's the List(Of T) constructor)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Insert at a line

    Quote Originally Posted by Evil_Giraffe View Post
    Even better would be to define a class to represent whatever this string ultimately is that exposes the Lines and a method for getting the full string out.
    Probably just a StringCollection and an extension method would be enough.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: Insert at a line

    Probably just a StringCollection and an extension method would be enough.
    Can you please explain how to do this?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Insert at a line

    Quote Originally Posted by Simon Canning View Post
    Can you please explain how to do this?
    I'm not going to explain anything about the StringCollection class or how to create an extension method because it is very easy to find information on them yourself. As for what the extension method should do:
    Code:
    Return String.Join(Environment.NewLine, myStringCollection.Cast(Of String)())
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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