What is the best practice for extracting a template from an en C # string and using it to create variables
Consider a line like this:
string myString = "C125:AAAAA|C12:22222|C16542:D1|ABCD:1234A|C6:12AAA"
I would like to end with something like this:
string C125 = "AAAAA";
string C12 = "22222";
string C16542 = "D1";
string C6 = "12AAA";
This means that I would like to extract substrings (or whatever) that match the "C + characters: characters" pattern (and exclude other patterns, ABCD:1234A
eg.). Then, automatically create a variable that will have the first part of my "substring" ( "C125:AAAAA"
eg) as the name (therefore the string C125
in this case) and the second part of my substring as the value ( "AAAAA"
eg).
What would be the best practice in C #? thank!
source to share
use a dictionary to store your values:
string myString = "C125:AAAAA|C12:22222|C16542:D1|ABCD:1234A|C6:12AAA";
Dictionary<string, string> result = new Dictionary<string, string>();
myString.Split('|').ToList().ForEach(x => result.Add(x.Split(':')[0], x.Split(':')[1]));
Update - improved solution from CodesInChaos:
string myString = "C125:AAAAA|C12:22222|C16542:D1|ABCD:1234A|C6:12AAA";
Dictionary<string, string> result = myString.Split('|').Select(x => x.Split(':')).ToDictionary(x => x[0], x => x[1]);
source to share
You can use the following regex, which will match any combination of word characters with a length of 1 or more that appears after :
.
@":(\w+)"
Note that the previous template used capture grouping, so you need to print the 1st group to get the correct result.
or you can use a positive appearance:
@"(?<=:)\w+"
But if you want to create a name from the first part, the best choice for such tasks would be to use a data structure, for example dictionary
.
So, you can iterate over the result if the following command:
Match match = Regex.Match(text, (\w+):(\w+));
And put pairs of 1st and 2nd groups in the dictionary.
source to share
If your goal is code generation, you can create a StringBuilder:
string myString = "C125:AAAAA|C12:22222|C16542:D1|ABCD:1234A|C6:12AAA";
var res = myString.Split('|')
.Select(s=>s.Split(':'))
.Where(arr=>arr[0][0] == 'C')
.Aggregate(new StringBuilder(),
(b, t)=>b.AppendFormat("string {0} = \"{1}\";", t[0], t[1])
.AppendLine());
output:
string C125 = "AAAAA";
string C12 = "22222";
string C16542 = "D1";
string C6 = "12AAA";
If your goal is to store values using keys, you can create a dictionary
string myString = "C125:AAAAA|C12:22222|C16542:D1|ABCD:1234A|C6:12AAA";
var D = myString.Split('|')
.Select(s=>s.Split(':'))
.Where(arr=>arr[0][0] == 'C')
.ToDictionary(arr=>arr[0], arr=>arr[1]);
output:
[C125, AAAAA]
[C12, 22222]
[C16542, D1]
[C6, 12AAA]
the format of the input string is not complicated, so String.Split is more appropriate here than RegEx
source to share
Perhaps you could use something like this. You can just use line splitting.
string myString = "C125:AAAAA|C12:22222|C16542:D1|ABCD:1234A|C6:12AAA";
StringBuilder result=new StringBuilder();
List<string> resulttemp = myString.Split('|').ToList();
foreach (string[] temp in from v in resulttemp where v.StartsWith("C") select v.Split(':'))
{
result.Append("string ");
result.Append(temp[0]);
result.Append("=");
result.Append("\"");
result.Append(temp[1]);
result.Append("\"");
result.Append(";");
result.Append("\n");
}
source to share