Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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= ???
Posted
Updated 2-Apr-13 1:19am
v2

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.

C#
// 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.
 
Share this answer
 
v3
Comments
Dionisios Karatzas 2-Apr-13 9:04am    
Thomas. Thank you for your code worked perfectly.
Can i request something?
Because confused a bit. Can you modify the code you posted to read this fields?:

//Weapon special ability grant (tariff application) <--- Value1
MultiSell_begin [weapon_variation_custom]<--- Value2 820<--- Value3
is_dutyfree = 0<--- Value4 "If Exists"
is_show_all = 0<--- Value5 "If Exists"
keep_enchanted = 1<--- Value6 "If Exists"
selllist={and <--- Value7
{{{[stormbringer_crt.anger];1}};{{[stormbringer];1};{[red_soul_crystal_5];1};{[gemstone_c];97}};{291000}};
{{{[stormbringer_focus];1}};{{[stormbringer];1};{[green_soul_crystal_5];1};{[gemstone_c];97}};{291000}}
}<--- till here
MultiSell_end

Thanks
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.
 
Share this answer
 
Regular expressions (System.Text.RegularExpressions.Regex class) can be helpful
 
Share this answer
 
v2
May be you can use RegEx to get

C#
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      string txt="//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";

      string re1=".*?";	// Non-greedy match on filler
      string re2="(nis_dutyfree)";	// Variable Name 1
      string re3=".*?";	// Non-greedy match on filler
      string re4="(=)";	// Any Single Character 1
      string re5=".*?";	// Non-greedy match on filler
      string re6="(\\d+)";	// Integer Number 1

      Regex r = new Regex(re1+re2+re3+re4+re5+re6,RegexOptions.IgnoreCase|RegexOptions.Singleline);
      Match m = r.Match(txt);
      if (m.Success)
      {
            String var1=m.Groups[1].ToString();
            String c1=m.Groups[2].ToString();
            String int1=m.Groups[3].ToString();
            Console.Write("("+var1.ToString()+")"+"("+c1.ToString()+")"+"("+int1.ToString()+")"+"\n");
      }
      Console.ReadLine();
    }
  }
}


but it will return nis_dutyfree = 1 but you can use substring.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900