Leon Winston
Leon Winston

Reputation: 141

Convert string into three letter Abbreviation

I've recently been given a new project by work to convert Any given string into 1-3 letter abbreviations. An example of something similar to what I must produce is below however the strings given could be anything:

switch (string.Name)
        {
            case "Emotional, Social & Personal": return "ESP";
            case "Speech & Language": return "SL";
            case "Physical Development": return "PD";
            case "Understanding the World": return "UW";
            case "English": return "E";
            case "Expressive Art & Design": return "EAD";
            case "Science": return "S";
            case "Understanding The World And It's People"; return "UTW";

}

I figured that I could use string.Split & count the number of words in the array. Then add conditions for handling particular length strings as generally these sentences wont be longer than 4 words however problems I will encounter are.

  1. If a string is longer than I expected it wouldn't be handled
  2. Symbols must be excluded from the abbreviation

Any suggestions as to the logic I could apply would be very appreciated. Thanks

Upvotes: 0

Views: 3953

Answers (4)

patrickhuie19
patrickhuie19

Reputation: 71

It seems that if it doesn't matter, you could just go for the simplest thing. If the string is shorter than 4 words, take the first letter of each string. If the string is longer than 4, eliminate all "ands", and "ors", then do the same.

To be better, you could have a lookup dictionary of words that you wouldn't care about - like "the" or "so".

You could also keep an 3D char array, in alphabetical order for quick lookup. That way, you wouldn't have any repeating abbreviations.

However, there are only a finite number of abbreviations. Therefore, it might be better to keep the 'useless' words stored in another string. That way, if the abbreviation your program does by default is already taken, you can use the useless words to make a new one.

If all of the above fail, you could start to linearly move through string to get a different 3 letter word abbreviation - sort of like codons on DNA.

Upvotes: 1

sujith karivelil
sujith karivelil

Reputation: 29036

Following snippet may help you:

string input = "Emotional, Social & Personal"; // an example from the question 
string plainText = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Regex.Replace(input, @"[^0-9A-Za-z ,]", "").ToLower()); // will produce a text without special charactors
string abbreviation = String.Join("",plainText.Split(" ".ToCharArray(),StringSplitOptions.RemoveEmptyEntries).Select(y =>y[0]).ToArray());// get first character from each word

Upvotes: 0

jdweng
jdweng

Reputation: 34429

Perfect place to use a dictionary

           Dictionary<string, string> dict = new Dictionary<string, string>() {
                {"Emotional, Social & Personal", "ESP"},
                {"Speech & Language","SL"},
                {"Physical Development", "PD"}, 
                {"Understanding the World","UW"},
                {"English","E"},
                {"Expressive Art & Design","EAD"},
                {"Science","S"},
                {"Understanding The World And It's People","UTW"}
            };

            string results = dict["English"];​

Upvotes: 0

juharr
juharr

Reputation: 32296

Something like the following should work with the examples you have given.

string abbreviation = new string(
    input.Split()
          .Where(s => s.Length > 0 && char.IsLetter(s[0]) && char.IsUpper(s[0]))
          .Take(3)
          .Select(s => s[0])
          .ToArray());

You may need to adjust the filter based on your expected input. Possibly adding a list of words to ignore.

Upvotes: 6

Related Questions