Results 1 to 11 of 11

Thread: [Resolved] IndexOf Text in Quotations

  1. #1

    Thread Starter
    Hyperactive Member ...:::ONE:::...'s Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    282

    Question [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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: IndexOf Text in Quotations

    Hmm...That's funny, it works for me:-
    vbnet Code:
    1. '
    2.         Dim txt As String = "Hello" & ChrW(34) & "Some text" & ChrW(34)
    3.  
    4.         Debug.WriteLine(txt.IndexOf(ChrW(34) & "Some text" & ChrW(34)))

    Output:-
    Code:
    5
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  6. #6

    Thread Starter
    Hyperactive Member ...:::ONE:::...'s Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    282

    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.
    6 Years

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: IndexOf Text in Quotations

    How about an extra space?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Hyperactive Member ...:::ONE:::...'s Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    282

    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.
    6 Years

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  11. #11

    Thread Starter
    Hyperactive Member ...:::ONE:::...'s Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    282

    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
  •  



Click Here to Expand Forum to Full Width