Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 2-Apr-13 2:18am
Edited 2-Apr-13 2:19am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v3
Comments
Dionisios Karatzas at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Regular expressions (System.Text.RegularExpressions.Regex class) can be helpful
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

May be you can use RegEx to get
 
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.
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100