Obtaining Values in a string

Posted on 2006-05-24
Last Modified: 2010-04-16
Hi All,

This is such a silly question, but how would i obtain the two values lat and lang in the string below, im looking at the two lat and lang values in center.

viewport: {center: {lat: 51.381443,lng: -2.357483},span: {lat: 0.089989,lng: 0.144182},


Question by:Jetski5822
    LVL 13

    Expert Comment

    I would use regular expressions to parse the results out of the string.

    Author Comment

    I have been trying but i jsut dont understand Regex very well, any ideas?
    LVL 6

    Accepted Solution


    You could try the following quickly knocked up regex...

    (using System.Text.RegularExpressions;)

    Regex r = new Regex(@".*center:.*{lat: ([-0123456789.]*),lng: ([-0123456789.]*).*lat");

    Then using the r.Match(yourstring).Group[1] for lat value and r.Match(yourstring).Group[2] for lng value.
    Console.WriteLine("lat=" + r.Match(@"viewport: {center: {lat: 51.381443,lng: -2.357483},span: {lat: 0.089989,lng: 0.144182},").Groups[1].ToString());
    Console.WriteLine("lng=" + r.Match(@"viewport: {center: {lat: 51.381443,lng: -2.357483},span: {lat: 0.089989,lng: 0.144182},").Groups[2].ToString());

    Hope this helps,
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    The old fashioned way...

                String input = "viewport: {center: {lat: 51.381443,lng: -2.357483},span: {lat: 0.089989,lng: 0.144182},";

                decimal lat = 0;
                decimal lng = 0;
                int center = input.IndexOf("center:");
                int leftBrace = input.IndexOf("{", center);
                int rightBrace = input.IndexOf("}", center);
                String data = input.Substring(leftBrace + 1, rightBrace - leftBrace - 1);
                String[] values = data.Split(",".ToCharArray());          
                String[] decimalValue = values[0].Split(":".ToCharArray());
                lat = decimal.Parse(decimalValue[1]);

                decimalValue = values[1].Split(":".ToCharArray());
                lng = decimal.Parse(decimalValue[1]);

                MessageBox.Show(lat.ToString() + Environment.NewLine + lng.ToString());

    Author Comment

    Awsome guys, thanks alot.. i changed it slightly.. as its much quicker now.

    Regex regVal = new Regex(@"center: {lat: ([-0-9.]*),lng: ([-0-9.]*)},", RegexOptions.IgnoreCase|RegexOptions.Compiled);

    Many thanks Nick

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now