|
-
Mar 26th, 2007, 06:46 PM
#1
Thread Starter
Addicted Member
[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:
How would I do this?
Thank You.
Last edited by GedOfEarthsea; Mar 26th, 2007 at 06:55 PM.
-
Mar 26th, 2007, 09:05 PM
#2
Thread Starter
Addicted Member
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?
-
Mar 26th, 2007, 10:20 PM
#3
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.
-
Mar 26th, 2007, 11:03 PM
#4
New Member
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
-
Mar 26th, 2007, 11:48 PM
#5
Re: [2005] Advanced String Manipulation
If you go the regex route then this should get you started:
(?<=")\w+(?=")
-
Mar 27th, 2007, 12:05 AM
#6
Re: [2005] Advanced String Manipulation
 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.
-
Mar 27th, 2007, 04:00 PM
#7
Thread Starter
Addicted Member
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?
-
Mar 27th, 2007, 04:12 PM
#8
Thread Starter
Addicted Member
Re: [2005] Advanced String Manipulation
Code:
theText = Replace(theText, Chr(13), " ")
theText = Replace(theText, Chr(9), " ")
That took care of that.
-
Mar 27th, 2007, 05:02 PM
#9
Thread Starter
Addicted Member
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.
-
Mar 27th, 2007, 05:15 PM
#10
Re: [2005] Advanced String Manipulation
vb Code:
Private Function GetQuotedSubstrings(ByVal str As String) As String()
Dim substrings As New List(Of String)
Dim startIndex As Integer = 0
Dim openQuoteIndex As Integer
Dim closeQuoteIndex As Integer
Do
'Get the index of the next opening quote.
openQuoteIndex = str.IndexOf(""""c, startIndex)
If openQuoteIndex = -1 Then
'There is no opening quote so there can be no closing quote.
closeQuoteIndex = -1
Else
'Start looking again immediately after the opening quote.
startIndex = openQuoteIndex + 1
'Get the index of the next closing quote.
closeQuoteIndex = str.IndexOf(""""c, startIndex)
End If
If closeQuoteIndex <> -1 Then
'Get the substring between the two quotes.
substrings.Add(str.Substring(openQuoteIndex + 1, _
closeQuoteIndex - openQuoteIndex - 1))
End If
'Start looking again immediately after the closing quote.
startIndex = closeQuoteIndex + 1
Loop Until closeQuoteIndex = -1 OrElse startIndex = str.Length
Return substrings.ToArray()
End Function
Here's an example of its use:
vb Code:
Dim str As String = "The quick ""brown"" fox jumps over the ""lazy"" ""dog"" ""and"" the ""cat"" ate ""the"" fox"
MessageBox.Show(String.Join(Environment.NewLine, _
Me.GetQuotedSubstrings(str)))
-
Mar 27th, 2007, 07:48 PM
#11
Thread Starter
Addicted Member
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.
-
Mar 27th, 2007, 07:55 PM
#12
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|