views:

170

answers:

3

I have a set of values based on which i have split the string

string[] seperator = new string[9];
        seperator[0] = "*";  //is the client
        seperator[1] = "/";  //is the name of company
        seperator[2] = "(";     //name of the market    
        seperator[5] = ":";    //ID
        seperator[6] = "?";    //orderType
        seperator[3] = "!@";   //realtive Time
        seperator[4] = "!+";   //
        seperator[7] = "+";    //quantity
        seperator[8] = "@";//price
string[] result = values.Split(seperator, StringSplitOptions.None);

For example: The input string is *A/AB(M!@12:6?SIMPLE!+5+2

OUTPUT
    [0]: ""
    [1]: "A"
    [2]: "AB"
    [3]: "M"
    [4]: "12"
    [5]: "6"
    [6]: "SIMPLE"
    [7]: "5"
    [8]: "2"

For example: The input string is *A(M!@12?SIMPLE!+5+2/AB:6

OUTPUT:
    [0]: ""
    [1]: "A"
    [2]: "M"
    [3]: "12"
    [4]: "SIMPLE"
    [5]: "5"
    [6]: "2"
    [7]: "AB"
    [8]: "6"

The problem I am facing is : how can I relate that A is the client, AB is the company..etc etc

as the order in which the user can enter this information RANDOM... If he doesnot enter any one of these values, it changes the result length ?

+5  A: 

What about using one or more regular expressions with named capture groups and indexing the matches by name?

Check for example this msdn page or this post.

Here is an example to get you started:

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main(string[] args) {

        Regex regex = new Regex(@"(?:\*(?<client>\w+))|(?:/(?<company>\w+))",RegexOptions.Compiled);
        string input = "*A/AB(M!@12:6?SIMPLE!+5+2";

        foreach (Match match in regex.Matches (input)) {
            if (match.Groups["client"].Success) {
                Console.WriteLine("Client = {0}", match.Groups["client"].Value);
            } else if (match.Groups["company"].Success) {
                Console.WriteLine("Company = {0}", match.Groups["company"].Value);
            }
        }


    }
}

I know that the syntax of the regular expressions can seem hard to understand at first, but they are a very powerful instrument whenever you need to do this kind of text operations.

Also, there are some tools that let you experiment and help you write regular expressions, like Expresso and The Regulator.

Paolo Tedesco
+1  A: 

Using something like this

SortedList<int, string> list = new SortedList<int, string>();
            string[] seperator = new string[9];
            seperator[0] = "*";  //is the client
            seperator[1] = "/";  //is the name of company
            seperator[2] = "(";     //name of the market    
            seperator[5] = ":";    //ID
            seperator[6] = "?";    //orderType
            seperator[3] = "!@";   //realtive Time
            seperator[4] = "!+";   //
            seperator[7] = "+";    //quantity
            seperator[8] = "@";//price
            string val = "*A/AB(M!@12:6?SIMPLE!+5+2";

            for (int iSep = 0; iSep < seperator.Length; iSep++)
                list.Add(val.IndexOf(seperator[iSep]), val);

will give you a list of positions where the seperators start, in any order the user inputs, and then you can use substring to retrieve the values

astander
Great...that worked!! Thanks astander and orsogufo
Tj
+3  A: 

What about performing a number of Replaces on the input string to get it into a more manageable format. For example.

inputString = inputString.Replace("*", ",Client=").Replace("/", ",Company=");

Then you can split on "," and get your list of strings with their headings and then split those on "=" to get the heading and the value.

Robin Day
this one worked too!!! Thanks
Tj