• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

parse for a string from input

I am receiving input from an email message where the user formats a message in a particular way and they we parse the message for the contents of whats in the quotes. I need to parse the below and return only whats in the quotes.

sfalsdafjk  asldjfkl  text="this is what should be returned"

The keyword is text=

Anything between "" after the text= should be returned to a variable etc....

How do I do this in vb.net
0
Dalexan
Asked:
Dalexan
  • 7
  • 7
1 Solution
 
rettiseertCommented:
Assuming that there will always be valid lines then this should work:

        Dim Line As String
        Dim Value As String
        Dim StartI As Integer
        Dim EndI As String

        Line = "sfalsdafjk  asldjfkl  text=""this is what should be returned"""

        StartI = Line.IndexOf("text=""") + 6
        EndI = Line.IndexOf("""", StartI)

        Value = Line.Substring(StartI, EndI - StartI)
0
 
jpaulinoCommented:
Try this way
        Dim str As String = "sfalsdafjk  asldjfkl  text=""this is what should be returned"""
        Dim posStart As Integer = str.IndexOf("""") + 1
        Dim posEnd As Integer = str.IndexOf("""", posStart)
 
        Dim result As String = str.Substring(posStart, posEnd - posStart)
 
        Debug.WriteLine(result)

Open in new window

0
 
DalexanAuthor Commented:
It needs to look for text= then afterwards anything in quotes.

The user may enter text=    "this is a test"  with blank spaces.

 it needs to test for text= first anywhere within a string then anything after the text= surrounded by quotes is what would be returned.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jpaulinoCommented:
You can have more then one value to read for line ?
Small change:

        Dim str As String = "sfalsdafjk  asldjfkl  text=""this is what should be returned"""
        Dim pos As Integer = str.IndexOf("text=")
        Dim posStart As Integer = str.IndexOf("""", pos) + 1
        Dim posEnd As Integer = str.IndexOf("""", posStart)
 
        Dim result As String = str.Substring(posStart, posEnd - posStart)
 
        Debug.WriteLine(result)

Open in new window

0
 
DalexanAuthor Commented:
Below is the code, the problem is, the message body has some other quote character instead of regular " characters...I think they are unicode, but not sure.  I need to allow for both.
    Private Sub tmrLCD_Text_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrLCD_Text.Tick
        Dim oApp As Outlook.Application = New Outlook.Application
        Dim sClassComp = "IPM.Note" ' String used for comparison with mail item.
 
        ' Get Mapi NameSpace.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
        Try
            ' Get Messages collection of Inbox.
            Dim oInbox As Outlook.MAPIFolder = oNS.Folders(1)
            oInbox = oInbox.Folders("LCD_Text")
            Dim oItems As Outlook.Items = oInbox.Items
            Console.WriteLine("Total : " & oItems.Count)
 
            ' Get unread e-mail messages.
            oItems = oItems.Restrict("[Unread] = true")
            Console.WriteLine("Total Unread : " & oItems.Count)
 
            ' Loop each unread message
            Dim oMsg As Outlook.MailItem
            Dim i As Integer
            For i = 1 To oItems.Count
                'Test to make sure item is a mail item
                'and not a meeting request.
                If oItems.Item(i).MessageClass = sClassComp Then
                    oMsg = oItems.Item(i)
                    Dim sMsg As String = oMsg.Body          '.Replace(System.Convert..ToChar(8221), """")
                    Dim pos As Integer = sMsg.IndexOf("text=", System.StringComparison.CurrentCultureIgnoreCase)
                    If pos >= 0 Then
                        Dim spos As Integer = sMsg.IndexOf("""") + 1
                        Dim epos As Integer = sMsg.IndexOf("""", spos)
                        lblLCD_Text.Text = sMsg.Substring(spos, epos - spos)
                        Select Case lblLCD_Text.ForeColor
                            Case Color.Red : lblLCD_Text.ForeColor = Color.Orange
                            Case Color.Orange : lblLCD_Text.ForeColor = Color.Gold
                            Case Color.Gold : lblLCD_Text.ForeColor = Color.Yellow
                            Case Color.Yellow : lblLCD_Text.ForeColor = Color.Lime
                            Case Color.Lime : lblLCD_Text.ForeColor = Color.Cyan
                            Case Color.Cyan : lblLCD_Text.ForeColor = Color.Magenta
                            Case Color.Magenta : lblLCD_Text.ForeColor = Color.Red
                        End Select
                    End If
                    oMsg.UnRead = False
                End If
            Next
 
            ' Clean up.
            oApp = Nothing
            oNS = Nothing
            oItems = Nothing
            oMsg = Nothing
        Catch ex As Exception
            lblLCD_Text.Text = String.Empty
        End Try
    End Sub

Open in new window

0
 
jpaulinoCommented:
What's the problem ? Can you show the text you have ?
0
 
DalexanAuthor Commented:
The body of the email is:
text=this is a test
but the quote characters are slanted.  I'm attaching a screenshot so you can see what it is...
screen.bmp
0
 
jpaulinoCommented:
Ok, try instead of sMsg.IndexOf("""")
sMsg.IndexOf(Chr(10)) or sMsg.IndexOf(Chr(13))
0
 
DalexanAuthor Commented:
chr(10) and chr(13) are CR and LF as I remember...I'm looking for something VB thinks is a double quote...sometimes.  
By doing this:
Dim sMsg As String = oMsg.Body.Replace(System.Convert.ToChar(8221), """")
I replace the email-style double quote with the standard chr(34).  This will work as long as there are not any other versions of a double quote out there.
0
 
jpaulinoCommented:
I din't told you to replace but to use in IndexOf()
0
 
DalexanAuthor Commented:
Is the quote a unicode character? I want to parse the string for whats between the quotes. I replace the email-style double quote with the standard chr(34).  This will work as long as there are not any other versions of a double quote out there. I dont want to hard code the line of code below to look for the quotes....

Dim sMsg As String = oMsg.Body.Replace(System.Convert.ToChar(8221), """")
0
 
DalexanAuthor Commented:
Anything on the above?
0
 
jpaulinoCommented:
Have you tried using IndexOf() ?
Can you attack and example ?
0
 
jpaulinoCommented:
typo

Can you attack an example ?
0
 
DalexanAuthor Commented:
Still have not figured out why the double quotes in the email body show up programmatically as ' instead of "
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now