Reputation: 701
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 ?
Upvotes: 1
Views: 448
Reputation: 57202
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.
Upvotes: 5
Reputation: 102478
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.
Upvotes: 3
Reputation: 166396
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
Upvotes: 1