0
string msg = "{Type=\"wednesday report\", corporate=\"ubl\", reg#=\"BNN - 527\", Driven=\"304.5Km\", MaxSpeed=\"150km / hr\", IgnitionsON=\"5\", Stopped=\"21.8hrs\", Running=\"1.7hrs\", Idle=\"0.5hrs\", image=\"varbinary data from db\", link=\"http://iteck.pk/d/pXhAo\"}";

string[] spitMsg = msg.Split('=');

I want

string type = "wednesday report";
vc 74
  • 37,131
  • 7
  • 73
  • 89
  • Seems like you need to split by comma before splitting by = – vc 74 Feb 09 '19 at 08:20
  • I have this string msg = @"Type=wednesday report , corporate= ubl , reg#= BNN - 527 , Driven= 304.5Km , MaxSpeed= 150km / hr , IgnitionsON= 5 , Stopped= 21.8hrs , Running= 1.7hrs , Idle= 0.5hrs , image= varbinary data from db , link= http://iteck.pk/d/pXhAo "; – adnan zafar Feb 09 '19 at 09:06

4 Answers4

0

I'm making a few assumptions about your data structure but something like this should work. It's deliberately manual so you can see what's going on:

        var data = "{Type=\"wednesday report\", corporate=\"ubl\", reg#=\"BNN - 527\", Driven=\"304.5Km\", MaxSpeed=\"150km / hr\", IgnitionsON=\"5\", Stopped=\"21.8hrs\", Running=\"1.7hrs\", Idle=\"0.5hrs\", image=\"varbinary data from db\", link=\"http://iteck.pk/d/pXhAo\"}";
        //let's remove the brackets
        data = data.Replace("{", "").Replace("}","");
        //then split by the comma to get the pieces
        var pieces = data.Split(',');
        //iterate through the pieces
        foreach (var piece in pieces)
        {
            //now we split by the = to get the key value pairs
            var kv = piece.Split('=');
            var key = kv[0];
            var value = kv[1];
            //viola, do what you want with them now
            Console.WriteLine(key + " is " + value);
        }

This doesn't handle cases where the quoted values contain commas, brackets or equal signs. That would throw off the splitting. A more powerful solution would be to use a regular expression that could account for these things.

mr.freeze
  • 13,731
  • 5
  • 36
  • 42
0

You provided two slightly different input strings (one in the original question, one in the comments). They're similar, so I just used the input from the original question. The input string in the comments would actually be slightly simpler to parse, since it contains fewer quotes and braces.

The examples are presented as xUnit tests, which you can easily copy and run to verify the logic.

This sort of parsing can brittle. If the input string contains a comma or equals sign as part of a key or value, the provided logic will fail. I've included an example of failed parsing in the code below.

Writing a parser for a proprietary format can be complex and error-prone. If you have control over the system that generates the strings, it might be better to use Json instead of a proprietary format. Json is a well-known format, and has good support from libraries like Json.NET.

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace SO_54604467
{
    public class Example
    {
        // This is the actual parsing logic
        Dictionary<string, string> Parse(string msg) =>
            msg.TrimStart('{')
                .TrimEnd('}')
                .Split(',')
                .Select(s => s.Split('='))
                .ToDictionary(
                    key => key[0].Trim(),
                    val => val[1].Trim('"')
                );

        [Fact]
        public void ParseExampleInput()
        {
            // This string was provided in the original question
            string msg = "{Type=\"wednesday report\", corporate=\"ubl\", reg#=\"BNN - 527\", Driven=\"304.5Km\", MaxSpeed=\"150km / hr\", IgnitionsON=\"5\", Stopped=\"21.8hrs\", Running=\"1.7hrs\", Idle=\"0.5hrs\", image=\"varbinary data from db\", link=\"http://iteck.pk/d/pXhAo\"}";

            // This is the data I would expect the message to parse into
            var expected = new Dictionary<string, string>
            {
                ["Type"] = "wednesday report",
                ["corporate"] = "ubl",
                ["reg#"] = "BNN - 527",
                ["Driven"] = "304.5Km",
                ["MaxSpeed"] = "150km / hr",
                ["IgnitionsON"] = "5",
                ["Stopped"] = "21.8hrs",
                ["Running"] = "1.7hrs",
                ["Idle"] = "0.5hrs",
                ["image"] = "varbinary data from db",
                ["link"] = "http://iteck.pk/d/pXhAo",
            };

            var actual = Parse(msg);

            Assert.Equal(expected, actual);
        }

        [Fact]
        public void DemonstrateFailureWithBadInput()
        {
            // This string fails, because it contains an unexpected comma
            string msg = "{Type=\"wednesday, report\"}";

            Assert.ThrowsAny<Exception>(() => Parse(msg));
        }
    }
}
xander
  • 1,689
  • 10
  • 18
0

Way #1: https://dotnetfiddle.net/Y4CAH3

using System;

public class Program
{
    public static void Main()
    {
        string msg = "{Type=\"wednesday report\", corporate=\"ubl\", reg#=\"BNN - 527\", Driven=\"304.5Km\", MaxSpeed=\"150km / hr\", IgnitionsON=\"5\", Stopped=\"21.8hrs\", Running=\"1.7hrs\", Idle=\"0.5hrs\", image=\"varbinary data from db\", link=\"http://iteck.pk/d/pXhAo\"}";
        string result = betweenStrings(msg,"Type=\"","\",");
        Console.WriteLine(result);
    }

    public static String betweenStrings(String text, String start, String end)
    {
        int p1 = text.IndexOf(start) + start.Length;
        int p2 = text.IndexOf(end, p1);

        if (end == "") return (text.Substring(p1));
        else return text.Substring(p1, p2 - p1);                      
    }
}

Way #2: https://dotnetfiddle.net/RJ4twI

using System;
using System.Text.RegularExpressions;                   
public class Program
{
    public static void Main()
    {
        string msg = "{Type=\"wednesday report\", corporate=\"ubl\", reg#=\"BNN - 527\", Driven=\"304.5Km\", MaxSpeed=\"150km / hr\", IgnitionsON=\"5\", Stopped=\"21.8hrs\", Running=\"1.7hrs\", Idle=\"0.5hrs\", image=\"varbinary data from db\", link=\"http://iteck.pk/d/pXhAo\"}";
        Match match = Regex.Match(msg, "Type=\"(.*?)\",");

        if (match.Success)
        {
            string key = match.Groups[1].Value;
            Console.WriteLine(key);
        }
    }
}
0

okey i can help you with that , it will get the ((first string)) which will be ("wednesday report")

    'textbox2 = {\"}
    'textbox3 = {\"}
    Dim sSource As String = RichTextBox1.Text 'String that is being searched
    Dim sDelimStart As String = TextBox2.Text 'First delimiting word
    Dim sDelimEnd As String = TextBox3.Text  'Second delimiting word
    Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
    Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd, nIndexStart + sDelimStart.Length + 1) 'Find the first occurrence of f2

    If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
        Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
        MessageBox.Show(res) 'Display
    Else
        MessageBox.Show("One or both of the delimiting words were not found!")
    End If

which will show message will say "wednesday report"

it's VB.net (you can convert it)

Dev I.A
  • 578
  • 4
  • 11