Reputation: 16851
I need to extract the values which are between the special characters &
.
string text = "&2&LL&1&likk&3&";
I need to extract the value 2, 1, 3
and insert the corresponding index of the List<string>
.
The list contains 10 elements:
0 - A
1 - B
2 - C
3 - D
...
9 - J
Finally when I substitute the list element to the string, it should be printed as CLLBlikkD. How can I do this ?
I tried splitting as follow:But, It only splits using the & sign.
string[] xx = text.Split('&');
Upvotes: 0
Views: 1377
Reputation: 22876
Regular expression alternative (17 is the difference from 'A' - '0'
) :
var result = Regex.Replace("&2&LL&1&likk&3&", "&[0-9]&", m => (char)(m.Value[1] + 17) + "");
Upvotes: 1
Reputation: 538
You should use regular expressions to do this, because regex is fun!
&\d& will match all of the digits between & symbols. Considering you're implying that letters and numbers should map together, we can adopt Henocs answer to to use regex for bonus points!
List<string> f = new List<string> ( ) { "A", "B", "C", "D", "E", "F", "G" };
string text = "&2&LL&1&likk&3&";
for ( int i = 0; i < f.Count; i++ )
{
text = text.RegexReplace ( text, "&" + i + "&", f[ i ] );
}
Console.WriteLine ( text );
By creating a regular expression of &(target digit)&, and replacing it with a letter from our string list, we will replace the entire block, rather than just the number.
Upvotes: -1
Reputation: 26
Hi you can try something like this
string text = "&2&LL&1&likk&3&";
string[] xx = text.Split('&');
string text2 = "";
string[] abc = { "A","B","C","D","E","F","G","H","I","J" };
for (int i = 0; i < xx.Length; i++)
{
text2 += xx[i];
}
for (int i = 0; i < abc.Length; i++)
{
text2 = text2.Replace(""+i, abc[i]);
}
MessageBox.Show(text2);
Upvotes: 0
Reputation: 1054
try this:
List<string> f = new List<string> ( ) { "A", "B", "C", "D", "E", "F", "G" };
string text = "&2&LL&1&likk&3&";
for ( int i = 0; i < f.Count; i++ )
{
text = text.Replace ( i.ToString ( ), f[ i ] );
}
text=text.Replace ( "&", "" );
Console.WriteLine ( text );
go through all the values in the list with a "FOR" and reasign the value of text with the value to replace
EDITED
List<string> f = new List<string> ( ) { "A", "B", "C", "D", "E", "F", "G" };
string text = "&2&LL&1&likk&3&";
for ( int i = 0; i < f.Count; i++ )
{
text = text.Replace ($"&{ i.ToString ( )}&", f[ i ] );
}
Console.WriteLine ( text );
Upvotes: 1