Question

Is there some class in C# that would behave like string and will allow me to store custom metadata (tags) for some characters/words?

For example, for a string Example string I might want to add information that capital letter E is capital letter and I might want to add type of some letters (say vocals).Then I might want to call .Replace or .Trim on the string and I need the result should still contain 'tags' for unchanged letters in the sequence.

Is something like that possible in C#/.NET without having to write all the logic myself?

Was it helpful?

Solution

The answer to "is there anything built-in" is no.

However, you can create your own class to deal with whatever metadata info structure you want and expose the string value as an inner property. You also can use it to override the ToString() method. This way you'll be able to pass around your object and still work with its string equivalent.

By overloading operators you'll also be able to do casting and comparisons with regular string instances.

OTHER TIPS

just for the fun of it!

to run this quickly:

  1. Download the always amazing LinqPad
  2. Open it and select Language C# Program
  3. paste the code below
  4. press Alt + X

void Main()
{
    string str = "Example, string";
    var output = Explain(str);

    OutputExplanation(output);
}

private void OutputExplanation(List<LetterExplanation> input)
{
    StringBuilder sb = new StringBuilder();

    foreach(var ltr in input)
        sb.AppendFormat("The letter {0} is {1}\n", ltr.Letter, ltr.Type);

    sb.ToString().Dump();
}

private List<LetterExplanation> Explain(string input) 
{
    var sb = new List<LetterExplanation>();

    foreach(char c in input.ToCharArray())
    {
        //c.Dump(); 
        LetterType type = LetterType.Character;

        // vowel, consonant or special
        if("aeiou".IndexOf(c.ToString(), StringComparison.InvariantCultureIgnoreCase) >= 0)
            type |= LetterType.Vowel;
        else if(" ,.-_<>/\\".IndexOf(c.ToString(), StringComparison.InvariantCultureIgnoreCase) >= 0)
            type |= LetterType.Special;
        else
            type |= LetterType.Consonant;

        // uppercase or lowercase
        if(char.IsUpper(c) && (type & LetterType.Special) != LetterType.Special)
            type |= LetterType.Uppercase;
        else if((type & LetterType.Special) != LetterType.Special)
            type |= LetterType.Lowercase;

        // add
        sb.Add(new LetterExplanation() { Letter = c, Type = type });
    }

    return sb;
} 

[Flags]
public enum LetterType {
    Vowel = 1, Consonant = 1 << 1, Uppercase = 1 << 2, Lowercase = 1 << 3, Number = 1 << 4, Special = 1 << 5, Character = 1 << 6
}

public class LetterExplanation
{
    public char Letter { get; set; }
    public LetterType Type { get; set; }
}

you will have an output of:

The letter E is Vowel, Uppercase, Character
The letter x is Consonant, Lowercase, Character
The letter a is Vowel, Lowercase, Character
The letter m is Consonant, Lowercase, Character
The letter p is Consonant, Lowercase, Character
The letter l is Consonant, Lowercase, Character
The letter e is Vowel, Lowercase, Character
The letter , is Special, Character
The letter   is Special, Character
The letter s is Consonant, Lowercase, Character
The letter t is Consonant, Lowercase, Character
The letter r is Consonant, Lowercase, Character
The letter i is Vowel, Lowercase, Character
The letter n is Consonant, Lowercase, Character
The letter g is Consonant, Lowercase, Character

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top