Results 1 to 6 of 6

Thread: Removing part of a string

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    35

    Removing part of a string

    I have a bunch of strings that have to have lines removed from them. I know the beginning and end words of each line, but I don't know what may be in between them.

    Is there any way in VB2005 to search through each string and look for the beginning marker and the end marker of the text that needs to be removed and then delete that and everything in between?

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Removing part of a string

    Why not just do:

    strVariable = strVariable.Replace(Environment.NewLine, "")
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Removing part of a string

    MetalKid, he wasn't real clear, but im not sure if the lines he is looking to remove are actually BLANK lines, or lines with text in them.

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    35

    Re: Removing part of a string

    The lines that need to be removed are actually xml tags of text. So I know what the beginning and end of the tag will be, I just don't know what will be in between. I guess that's where the problem is. I've seen the string replace function, but I thought you could only use that if you knew exactly what you wanted to replace.

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Removing part of a string

    Probably wants the latter. Probably using a mixture of the IndexOf and Substring methods would work to get what you want or regular expressions if you are feeling up to it.

    EDIT: I think XML has something about InnerText and OuterText and getting them. Search the forum for something like that. Someone complained the other day that all they did was post the solution to a problem like this.

    You should have mentioned XML in post 1. Better description of problem = quicker/better solutions
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Removing part of a string

    if the entire document is an XML document you should use the classes in the XML namespace like the XMLDocument class to load the XML file.

    The XML classes have an extensive library of functions for adding/removing/editing the XML nodes in the document, and when you are done, you simply call the save method and save the file back to disk.

    Sure you could do it with a bunch of messy string manipulation, but why bother when you could code it way cleaner, and learn all about the XML classes in the process.

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