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('=');
我想要
string type = "wednesday report";
答案 0 :(得分:0)
我正在对您的数据结构做一些假设,但是类似的事情应该起作用。这是故意的手动操作,因此您可以查看发生了什么:
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);
}
这不适用于引用的值包含逗号,方括号或等号的情况。那将消除分裂。一个更强大的解决方案是使用可以解决这些问题的regular expression。
答案 1 :(得分:0)
您提供了两个略有不同的输入字符串(一个在原始问题中,一个在注释中)。它们是相似的,因此我只使用了原始问题的输入。由于注释中的输入字符串包含较少的引号和花括号,因此它实际上实际上更易于解析。
这些示例以xUnit测试的形式呈现,您可以轻松地复制并运行它们以验证逻辑。
这种解析可能很脆弱。如果输入字符串包含逗号或等号作为键或值的一部分,则提供的逻辑将失败。我在下面的代码中包含了解析失败的示例。
为专有格式编写解析器可能很复杂且容易出错。如果您可以控制生成字符串的系统,则最好使用Json而不是专有格式。 Json是一种众所周知的格式,并得到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));
}
}
}
答案 2 :(得分:0)
方法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);
}
}
方法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);
}
}
}
答案 3 :(得分:0)
好,我可以为您提供帮助,它会得到((第一个字符串)),它是(“星期三报告”)
/*Model Object*/
export Class StudentModel{
Name:string
}
@Component(....)
export Class OneComponent implements OnInit{
public validateForm :FormGroup;
public studentModel:StudentModel={ Name:'XiaoMing' }
constructor(
private _fb: FormBuilder
) {}
ngOnInit(): void {
/**表单初始赋值 */
this.validateForm = this._fb.group({
Name: [this.studentModel.Name]
})
}
}
将显示消息的内容为“星期三报告”
这是VB.net(您可以将其转换)