Solved

Parse string of data

Posted on 2013-11-22
5
210 Views
Last Modified: 2013-11-26
How can I programmtically parse to get the "weather-type" value from this string

<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">

I need to do this in VB.NET and return the value for the weather-type element

Any help would be appreciated
Thanks
JK
0
Comment
Question by:jknj72
5 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 39669543
is this homework?

you could use Instr to find the start of  Weather type then add 14 to get the start position of the data.... after that you then need to copy all the c haracters until you find the next quote/ending quote

see http://social.msdn.microsoft.com/Forums/en-US/6f64af4e-1cdf-46ee-8a14-6021c2d64951/vbnet-search-for-stringword-in-strings
0
 
LVL 40
ID: 39669746
Dim startIndex As Integer = yourString.IndexOf("weather-type=") + 14
            Dim endIndex As Integer = yourString.IndexOf("""", startIndex)
            Dim result As String = yourString.Substring(startIndex, endIndex - startIndex)
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39669870
Hi jk;

You could also use a regular expression to get the value as shown below.

' The string you wish to parse
Dim weatherStr As String = "Your Input String Here."
' Get the weather-type from the string
Dim weather = Regex.Match(weatherStr, "(?i)weather-type=""([^""]+)")
' Test to see if weather-type was in the string and if so access the value
If weather.Success Then
    Console.WriteLine(weather.Groups(1).Value)
End If

Open in new window

0
 

Author Comment

by:jknj72
ID: 39675098
Thanks I will try these out today.

@Lowfatspread - not homework, its work
0
 

Author Closing Comment

by:jknj72
ID: 39677546
Thanks Fernando. Worked like a charm
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question