Нужно чтобы в значении выводилось цифра после N, а в ключе цифры после V
Нужно чтобы в значении выводилось цифра после N, а в ключе цифры после V. Что не так? в значении выводится B, а не 0
internal class Program
{
static void Main(string[] args)
{
string input = "N0B3W300V150N0B3W300V400N0B3W300V650N0B3W300V850N2B1W600V2500N0B3W300V2150N0B3W300V2400N0B3W300V2650N0B3W300V2850N0B3W300V4150N0B3W300V4400N0B3W300V4650N0B3W300V4850";
string[] words = SplitText(input);
Dictionary<string, char> resultDict = CreateDictionary(words);
PrintDictionary(resultDict);
}
static string[] SplitText(string text)
{
string[] words = text.Split(new string[] { "V", "N" }, StringSplitOptions.RemoveEmptyEntries);
return words;
}
static Dictionary<string, char> CreateDictionary(string[] words)
{
Dictionary<string, char> coordinatesDictionary = new Dictionary<string, char>();
for (int i = 1; i < words.Length; i += 2)
{
string coordinate = words[i].Substring(0);
char value = words[i - 1][1];
coordinatesDictionary[coordinate] = value;
}
return coordinatesDictionary;
}
static void PrintDictionary(Dictionary<string, char> dictionary)
{
foreach (var entry in dictionary)
{
Console.WriteLine($"Ключ: {entry.Key}, Значение: {entry.Value}");
}
Console.ReadKey();
}
}
Ответы (1 шт):
Автор решения: MarkShcerbakov
→ Ссылка
Решение на основе регулярки:
using System.Linq;
using System.Text.RegularExpressions;
var result = Regex.Matches(input, @"N(\d+).+?V(\d+)(?=N|$)")
.ToDictionary(m => m.Groups[2].Value, m => m.Groups[1].Value);