Question

I am stuck at a function which checks a string ('14534000000875e') if it contains a letter. If it contains a letter (a-z), remove the letter and add a string to the end.

To realize this, I have created a Dictionary<char, string> Pairs which has mapped a to 09001, b to 09002 [...] and z to 09026

This is my code so far:

public static string AlterSerial(string source)
{
    Dictionary<char, string> pairs = new Dictionary<char, string>();
    pairs.Add('a', "09001");
    ...

    int index = source.IndexOf(x);
    if (index != -1)
    {
        return source.Remove(index, 1);
    }
    return source;
}

How can I check if the source string contains one of the 26 keys, delete this key and add the corresponding string to the end of the source-string?

Note: the letter is not always at the end of the source.

Kind regards

Was it helpful?

Solution

Try this:

Dictionary<char, string> pairs = new Dictionary<char, string>();
pairs.Add('a', "09001");
...

foreach(KeyValuePair<string, string> entry in pairs)
{
    if (source.Contains(entry.Key))  // .Key must be capitalized
    {
      source = source.Replace(entry.Key, "") + entry.Value;
      break;
    }
}

return source;
....

OTHER TIPS

So this is the solution where you have only one letter within your string. You need to find the letter that is within the string, if it exists, and find its index. Then you have to insert the respective value instead of the letter, obtained from the dictionary.

String mystring = "1453400e0000875";
Dictionary<char, string> pairs = new Dictionary<char, string>();
pairs.Add('a', "09001");
pairs.Add('b', "09002");
pairs.Add('c', "09003");
pairs.Add('d', "09004");
pairs.Add('e', "09005");
//...
char letter = mystring.FirstOrDefault(a => Char.IsLetter(a));
if (letter != '\0')
{
      int index = mystring.IndexOf(letter);
      string substitute;
      pairs.TryGetValue(mystring[index], out substitute);
      mystring = mystring.Substring(0, index) + substitute + mystring.Substring(index + 1);
}

EDIT: Using Replace method of the string, the if part can be altered like this:

char letter = mystring.FirstOrDefault(a => Char.IsLetter(a));
if (letter != '\0')
{
       string substitute;
       pairs.TryGetValue(letter, out substitute);
       mystring = mystring.Replace(letter.ToString(), substitute);
}

EDIT2:And if I didn't understand the OP correctly so that he wants to remove the letter and add the replacement string to the end of the source string, the if statement should be like this:

if (letter != '\0')
{
       string substitute;
       pairs.TryGetValue(letter, out substitute);
       mystring = mystring.Replace(letter.ToString(), "");
       mystring += substitute;
}

and this is the generalisation when you have more letters within the string. It is the similar solution but requires to iterate over all the letters within the string.

String mystring = "1453a400e0000b875";
Dictionary<char, string> pairs = new Dictionary<char, string>();
pairs.Add('a', "09001");
pairs.Add('b', "09002");
pairs.Add('c', "09003");
pairs.Add('d', "09004");
pairs.Add('e', "09005");
//...
var lettersList = mystring.Where(a => Char.IsLetter(a));
foreach (char letter in lettersList)
{
        int index = mystring.IndexOf(letter);
        string substitute;
        pairs.TryGetValue(mystring[index], out substitute);
        mystring = mystring.Substring(0, index) + substitute + mystring.Substring(index + 1);
}

Checked and it works!

Nikola's answer is good and if you like it you should mark it as the answer. However I like this simple method (slightly borrowed a few things :p).

var Alphabet = "abcdefghijklmnopqrstuvwxyz".ToArray();

var Pairs = new Dictionary<char, string>();
for(var i = 1; i < Alphabet.Count() +1; i++)
    Pairs.Add(Alphabet[i-1], "090" + (i < 10 ? "0" + i.ToString() : i.ToString()));

var Source = "14534000000875e";

var Chars = Source.Where(x => Char.IsLetter(x));
var Output = string.Empty();

foreach(var Char in Chars)
    Output = Source.Replace(Char.ToString(), Pairs[Char]);

or if you want the replacement at the end and only once for repeated chars?

foreach(var Char in Chars)
Output = Source.Replace(Char.ToString(),"") + (Pairs[Char]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top