Results 1 to 12 of 12

Thread: [RESOLVED] [2005] Advanced String Manipulation

  1. #1

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Resolved [RESOLVED] [2005] Advanced String Manipulation

    Hello, I am trying to do some advanced string manipulation. I need to go through about a multi-lined textbox and take out everything but what is not inside quotes "". I would then like to list those objects.

    For example this is what is in the textbox:

    The quick "brown" fox jumps over the "lazy" "dog" "and" the "cat" ate "the" fox...
    I want it to then just list the quoted objects into a listbox like so:

    brown
    lazy
    dog
    and
    cat
    the
    And then after that I want the first quote and every other one only:

    brown
    dog
    cat
    How would I do this?

    Thank You.
    Last edited by GedOfEarthsea; Mar 26th, 2007 at 06:55 PM.

  2. #2

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [2005] Advanced String Manipulation

    I know I can do most of that stuff, I just wanted to know if I can remove everything that isn't in the quotes then I can replace the spaces to nothing, and the quotes to a new paragraph.

    How would I remove everything that is not within quotes?

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

    Re: [2005] Advanced String Manipulation

    You may be able to do it with regular expressions but using string manipulation it's quite easy. Just use a loop and String.IndexOf to get each double quote index in turn. For each second double quote you use those indexes to get the substring in between. Give it a go and see what you come up with. Make sure you read the doco for String.Substring first because it's overloaded.
    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

  4. #4
    New Member
    Join Date
    Mar 2007
    Posts
    2

    Re: [2005] Advanced String Manipulation

    use replate function to remove quotes
    then upload the text into array use:

    YourText = replace(YourText,"""") 'double quote represent a quote...
    MyArrForListBox = split(YourText," ") 'split by space

    then upload the array to listbox with your way..

    let me know if still can't work

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: [2005] Advanced String Manipulation

    If you go the regex route then this should get you started:
    (?<=")\w+(?=")

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

    Re: [2005] Advanced String Manipulation

    Quote Originally Posted by Edneeis
    If you go the regex route then this should get you started:
    (?<=")\w+(?=")
    I was thinking about regex and I wonder whether it would also return the bits "outside" the quotes, e.g.
    Code:
    start "inside" outside "inside" end
    The word outside is between two double quotes too, even though to us it is outside the quotes. I wonder whether a regex would return that too. Maybe not because the second double quote may already have been consumed by the first match. Only one way to find out I suppose, but as it's not my quest I'll leave that to others.
    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
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [2005] Advanced String Manipulation

    I have no idea what regex "regular expressions" are, so I am going to try it the other way really quickly and then I will look into the other way. I am trying to split at the spaces, but there seems to be a problem, there is more than a space, it is a dark bar, which is a new line when I copy and paste... I don't know why it isn't a new line in my textbox. How would I replace that symbol with something?

  8. #8

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [2005] Advanced String Manipulation

    Code:
    theText = Replace(theText, Chr(13), " ")
    theText = Replace(theText, Chr(9), " ")
    That took care of that.

  9. #9

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [2005] Advanced String Manipulation

    I have got to making the list, but now after the first line in the list I need to remove 8 lines, save the next line and delete 7 lines, and I need to do that until the end.

    Is there a way to do that?

    ***
    Nevermind about that, my two year old cousin was playing with my keyboard when I got something to drink bc I am missing like three lines and jibberish was added and now I can't remember what I had.

    Just my luck.
    Last edited by GedOfEarthsea; Mar 27th, 2007 at 05:09 PM.

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

    Re: [2005] Advanced String Manipulation

    vb Code:
    1. Private Function GetQuotedSubstrings(ByVal str As String) As String()
    2.     Dim substrings As New List(Of String)
    3.     Dim startIndex As Integer = 0
    4.     Dim openQuoteIndex As Integer
    5.     Dim closeQuoteIndex As Integer
    6.  
    7.     Do
    8.         'Get the index of the next opening quote.
    9.         openQuoteIndex = str.IndexOf(""""c, startIndex)
    10.  
    11.         If openQuoteIndex = -1 Then
    12.             'There is no opening quote so there can be no closing quote.
    13.             closeQuoteIndex = -1
    14.         Else
    15.             'Start looking again immediately after the opening quote.
    16.             startIndex = openQuoteIndex + 1
    17.  
    18.             'Get the index of the next closing quote.
    19.             closeQuoteIndex = str.IndexOf(""""c, startIndex)
    20.         End If
    21.  
    22.         If closeQuoteIndex <> -1 Then
    23.             'Get the substring between the two quotes.
    24.             substrings.Add(str.Substring(openQuoteIndex + 1, _
    25.                                          closeQuoteIndex - openQuoteIndex - 1))
    26.         End If
    27.  
    28.         'Start looking again immediately after the closing quote.
    29.         startIndex = closeQuoteIndex + 1
    30.     Loop Until closeQuoteIndex = -1 OrElse startIndex = str.Length
    31.  
    32.     Return substrings.ToArray()
    33. End Function
    Here's an example of its use:
    vb Code:
    1. Dim str As String = "The quick ""brown"" fox jumps over the ""lazy"" ""dog"" ""and"" the ""cat"" ate ""the"" fox"
    2.  
    3. MessageBox.Show(String.Join(Environment.NewLine, _
    4.                             Me.GetQuotedSubstrings(str)))
    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

  11. #11

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [2005] Advanced String Manipulation

    Wow, thanks a lot for the help. That code looks a little advanced for me. I used vb6 for like 2 years, then last year I got into 2005, so I am still new to visual basic and I don't use it as much as I would like, I get too caught up in books and games, and web development. I hope one day I will be able to help some of you like I am helped out when I need it.

    Thanks again.

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

    Re: [2005] Advanced String Manipulation

    Every line of code in my previous post is, in itself, very simple. If you follow the proper procedures then you'd come up with the same thing, the proper procedures being:

    1. Assume that you have to perform the operation using pen and paper. Write down, in words, the steps you would take.
    2. Convert those written steps to pseudo-code.
    3. Implement your pseudo-code in VB.

    If you look at the code it makes perfect sense.

    1. Start at the beginning of the string.
    2. Move forward until you find an opening quote.
    3. Continue moving forward from the next character until you find a closing quote.
    4. Get the substring between the quotes and add it to a list.
    5. If you aren't at the end of the string then go back to step 2.

    That's what programming teachers tell you to do and that's why. Going from an idea to VB code is all but impossible for beginners, but it's beginners who are most reticent to follow the proper steps. Such is human nature. I assure you that I always followed those steps when I was learning. Well, almost always.
    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