|
-
Jun 2nd, 2013, 08:59 AM
#1
Thread Starter
Hyperactive Member
[Resolved] IndexOf Text in Quotations
I'm trying to find the index of "some text", including the quotations. I've tried a few ways to escape the quotations but all have failed, resulting in an index of -1
Example...
Code:
Dim txtFile as String
Dim txtIndex as Integer
txtFile = 'Some text file
txtIndex = txtFile.IndexOf(Chr(34) & "some text" & Chr(34)) ' Index = -1, failed.
txtIndex = txtFile.IndexOf("""some text""") ' Index = -1, failed.
Is there anyway to get the index of a text that contains quotes?
Thanks,
-Chris
Last edited by ...:::ONE:::...; Jun 2nd, 2013 at 10:29 AM.
6 Years
-
Jun 2nd, 2013, 09:08 AM
#2
Re: IndexOf Text in Quotations
try this:
Code:
Dim txtFile As String
Dim txtIndex As Integer
txtFile = "Some text file containing ""some text"""
txtIndex = txtFile.IndexOf("""some text""")
MsgBox(txtIndex) 'index = 26
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 2nd, 2013, 09:09 AM
#3
Re: IndexOf Text in Quotations
Hmm...That's funny, it works for me:-
vbnet Code:
'
Dim txt As String = "Hello" & ChrW(34) & "Some text" & ChrW(34)
Debug.WriteLine(txt.IndexOf(ChrW(34) & "Some text" & ChrW(34)))
Output:-
-
Jun 2nd, 2013, 09:11 AM
#4
Re: IndexOf Text in Quotations
I tried it with a real txt file too. this is the exact contents of the file:
Some text file containing "some text"
+ this is the code:
Code:
Dim txtFile As String
Dim txtIndex As Integer
txtFile = IO.File.ReadAllText("C:\Users\Paul\Desktop\1.txt")
txtIndex = txtFile.IndexOf("""some text""")
MsgBox(txtIndex) 'index = 26
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 2nd, 2013, 09:15 AM
#5
Re: IndexOf Text in Quotations
Which all suggests that the OP was getting the right result, even though it was not expected. The next step would be to take a careful look at what is intxtFile, because it is sounding like it doesn't hold what you think it holds. All it would take would be a single space between the quotes and the string and it will not match. A single extra space might be pretty hard to find. Other problems could also exist.
My usual boring signature: Nothing
 
-
Jun 2nd, 2013, 09:16 AM
#6
Thread Starter
Hyperactive Member
Re: IndexOf Text in Quotations
That code works by itself, but with my text file converted to a string, it does not.
I can accurately get the indexes of any other text that does not contain quotes, and verification of the text file itself shows that quotes do indeed exist in the file. Also if I convert the file to a string and send it to a richtextbox I can also see the quotes. Unless VB is converting the all quotes inside the string somehow and converting them back when the string is displayed.
-
Jun 2nd, 2013, 09:17 AM
#7
Re: IndexOf Text in Quotations
How about an extra space?
My usual boring signature: Nothing
 
-
Jun 2nd, 2013, 09:35 AM
#8
Thread Starter
Hyperactive Member
Re: IndexOf Text in Quotations
I apologize for simplifying my problem, I thought it would make it easier, but it looks like it may have stirred up some confusion.
I'm really working with WebClient and converting a URL into an (html)text string
Code:
WebClient.DownloadString("somehtmlurl")
This code, when used like this...
Code:
Dim someHTML as string
someHTML = WebClient.DownloadString("somehtmlurl")
RichTextBox1.text = someHTML
...provides me with all the text of the html document on the url, just as if I were to open it notepad. The url converted to a string does show a lot of "extra space", tabs, indentation, etc., but I didn't think this would be a problem if it can be displayed accurately after being converted to a string.
-
Jun 2nd, 2013, 09:38 AM
#9
Re: IndexOf Text in Quotations
Quotation marks are notorious for being some odd-unicode value.
Have you examined the ASCW value of the character to see if in fact it's a 34??
-
Jun 2nd, 2013, 09:40 AM
#10
Re: IndexOf Text in Quotations
szlamany may well be right.
I've noticed some dodgy quote marks, on or in html pages, that look right but aren't
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 2nd, 2013, 10:28 AM
#11
Thread Starter
Hyperactive Member
Re: IndexOf Text in Quotations
I figured out my problem. A variable at the top of my class was producing a null value which was used in my url builder, this creates the url used for the WebClient.DownloadString function. The null value changed the url in the WebClient.DownloadString function, and caused the downloader to add the wrong html to the html string.
Looks like this was my fault guys. It seems I spent too many hours coding today, I think I need a break.
The best way to be sure to represent a quote in a string is to add chr(34) to the string line of a function where the quotes would be.
Code:
String.IndexOf(Chr(34) & "Some Text In Quotes" & Chr(34))
Thanks for the help,
-Chris
Last edited by ...:::ONE:::...; Jun 2nd, 2013 at 10:32 AM.
6 Years
Tags for this Thread
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
|