从String中获取Param [英] Get Param from String
问题描述
你好我有一个来自文件的字符串并存储在这样的字符串var上:
//生命宽度管武器\\\
nMultiSell_begin \\ \\ t [blackmerchant_weapon] \t1\r\\\
is_dutyfree = 1 \\\\ nnlllllist = {\\\\ {{{[flamberge]; 1}}; {{[crystal_c]; 573}; {[crystal_d]; 2865}}}; \r\\\
{{{[akat_long_bow]; 1}}; {{[crystal_c]; 1075}; {[crystal_d]; 5375}}}; \r\\ \
{{{[orcish_glaive]; 1}}; {{[crystal_c]; 573}; {[crystal_d]; 2865}}}; \r\\\
{{{[斧砍]; 1}}; { {[crystal_c]; 1075}; {[crystal_d]; 5375}}} \\\\ n}} \\\\
MultiSell_end \\ n
如何从这个字符串中获取一个参数,例如is_dutyfree = 1或selllist = ???
我已经为你做了一些事情。
请注意我没有加入任何检查,例如检查数组边界等。
这是有效的你提供的字符串。
// 包含一个条目
class SellEntry
{
public SellEntry( string name, string value )
{
this .Name = name;
此 .Value = value ;
}
public string 名称{获得跨度>; set ; }
public string 值{ get 跨度>; set ; }
}
// 持有多卖记录
class MultiSellRecord
{
public MultiSellRecord( string value1, string value2, string isDutyFree)
{
此 .Value1 = value1;
this .Value2 = value2;
this .IsDutyFree = isDutyFree;
此 .SellList = new 列表< list>< SellEntry>>();
}
public string Value1 {获得跨度>; set ; }
public string Value2 { get 跨度>; set ; }
public string IsDutyFree { get 跨度>; set ; }
public 列表< list>< SellEntry>> SellList { get ; set ; }
}
// 读取/解析/转换提供的字符串的方法由你
MultiSellRecord Read( string str)
{
string [] split = str.Split( new [] {Environment.NewLine, < span class =code-string> \t},StringSplitOptions.RemoveEmptyEntries);
int dutyIndex = split [ 4 ]。IndexOf( =);
string isDutyFree = split [ 4 ]。Substring(dutyIndex + 1 跨度>)修剪();
string value1 = split [ 2 ]。替换( [, string .Empty).Replace(< span class =code-string> ], string .Empty);
MultiSellRecord multiEntry = new MultiSellRecord(value1,split [ 3 ],isDutyFree );
for ( int i = 6 ; i < split.Length; i ++)
{
if (split [i] == })
{
break ;
}
var list = split [i] .Split( new [] { {, }, ; , [, ]},StringSplitOptions.RemoveEmptyEntries);
var sellList = new List< SellEntry>();
for ( int j = 0 ; j < list.Length; j + = 2 )
{
sellList.Add( new SellEntry(list [j],list [j + 1 ]));
}
multiEntry.SellList.Add(sellList);
}
return multiEntry;
}
再见,
Thomas。
您需要将字符串拆分为标记,可能是重复使用 string.Split
方法。然后在生成的标记中搜索特定的关键字。
正则表达式(System.Text.RegularExpressions.Regex类)可能会有所帮助
Hello i have a string from a file and stored on a string var like this:
"//Life width tube weapon\r\nMultiSell_begin\t[blackmerchant_weapon]\t1\r\nis_dutyfree = 1\r\nselllist={\r\n{{{[flamberge];1}};{{[crystal_c];573};{[crystal_d];2865}}};\r\n{{{[akat_long_bow];1}};{{[crystal_c];1075};{[crystal_d];5375}}};\r\n{{{[orcish_glaive];1}};{{[crystal_c];573};{[crystal_d];2865}}};\r\n{{{[poleaxe];1}};{{[crystal_c];1075};{[crystal_d];5375}}}\r\n}\r\nMultiSell_end\r\n"
How can i get a param from this string for example is_dutyfree = 1 or selllist= ???
Hi,
I ''hacked'' something up for you.
Please be aware that I didn''t incorporate any checks, such as checking for array bounds etc.
This works for the string you provided.
// contains a single entry class SellEntry { public SellEntry(string name, string value) { this.Name = name; this.Value = value; } public string Name { get; set; } public string Value { get; set; } } // holds a multi sell record class MultiSellRecord { public MultiSellRecord(string value1, string value2, string isDutyFree) { this.Value1 = value1; this.Value2 = value2; this.IsDutyFree = isDutyFree; this.SellList = new List<list><SellEntry>>(); } public string Value1 { get; set; } public string Value2 { get; set; } public string IsDutyFree { get; set; } public List<list><SellEntry>> SellList { get; set; } } // method to read/parse/convert the string provided by you MultiSellRecord Read(string str) { string[] split = str.Split(new[] { Environment.NewLine, "\t" }, StringSplitOptions.RemoveEmptyEntries); int dutyIndex = split[4].IndexOf("="); string isDutyFree = split[4].Substring(dutyIndex + 1).Trim(); string value1 = split[2].Replace("[", string.Empty).Replace("]", string.Empty); MultiSellRecord multiEntry = new MultiSellRecord(value1, split[3], isDutyFree); for (int i = 6; i < split.Length; i++) { if (split[i] == "}") { break; } var list = split[i].Split(new[] { "{", "}", ";", "[", "]" }, StringSplitOptions.RemoveEmptyEntries); var sellList = new List<SellEntry>(); for (int j = 0; j < list.Length; j += 2) { sellList.Add(new SellEntry(list[j], list[j + 1])); } multiEntry.SellList.Add(sellList); } return multiEntry; }
Bye,
Thomas.
You need to split your string into tokens, probably by repeated use of the string.Split
method. Then search for specific keywords in the resulting tokens.
Regular expressions (System.Text.RegularExpressions.Regex class) can be helpful
这篇关于从String中获取Param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!