Text transforms on all of objects properties, is there a better way?

Error processing SSI file

Answers

  1. Grassi

    • 2017/8/21

    I wrote a StringTemplate class which could probably be modified to suit your needs... It behaves like String.Format, with a major difference : you can use names for placeholders, rather than indexes. The values to format can be specified as either a IDictionary<string, object>, or any object (in that case each placeholder will be replaced with the value of the property with the same name).

    For instance :

    // with a dictionary :
    var values = new Dictionary<string, object>
    {
        { "Title", "Mr." },
        { "LastName", "Smith" }
    };
    string a = StringTemplate.Format("Hello {Title} {LastName}", values);
    
    // with an anonymous type :
    string b = StringTemplate.Format(
        "Hello {Title} {LastName}",
         new { Title = "Mr.", LastName = "Smith" });
    

    If you need to use the same template several times, you can create an instance of StringTemplate and reuse it for better performance (the template string will be parsed only once).

    You can also specify format modifiers, like in String.Format.

    To fit your exact needs, this class will need a few adjustments, but it shouldn't be too hard...


    Here's the code :

    public class StringTemplate
    {
        private string _template;
        private static Regex _regex = new Regex(@"(?<open>{+)(?<key>\w+)(?<format>:[^}]+)?(?<close>}+)", RegexOptions.Compiled);
    
        public StringTemplate(string template)
        {
            template.CheckArgumentNull("template");
            this._template = template;
            ParseTemplate();
        }
    
        private string _templateWithIndexes;
        private List<string> _placeholders;
    
        private void ParseTemplate()
        {
            _placeholders = new List<string>();
            MatchEvaluator evaluator = (m) =>
            {
                if (m.Success)
                {
                    string open = m.Groups["open"].Value;
                    string close = m.Groups["close"].Value;
                    string key = m.Groups["key"].Value;
                    string format = m.Groups["format"].Value;
    
                    if (open.Length % 2 == 0)
                        return m.Value;
    
                    open = RemoveLastChar(open);
                    close = RemoveLastChar(close);
    
                    if (!_placeholders.Contains(key))
                    {
                        _placeholders.Add(key);
                    }
    
                    int index = _placeholders.IndexOf(key);
                    return string.Format("{0}{{{1}{2}}}{3}", open, index, format, close);
                }
                return m.Value;
            };
            _templateWithIndexes = _regex.Replace(_template, evaluator);
        }
    
        private string RemoveLastChar(string str)
        {
            if (str.Length > 1)
                return str.Substring(0, str.Length - 1);
            else
                return string.Empty;
        }
    
        public static implicit operator StringTemplate(string s)
        {
            return new StringTemplate(s);
        }
    
        public override string ToString()
        {
            return _template;
        }
    
        public string Format(IDictionary<string, object> values)
        {
            values.CheckArgumentNull("values");
    
            object[] array = new object[_placeholders.Count];
            for(int i = 0; i < _placeholders.Count; i++)
            {
                string key = _placeholders[i];
                object value;
                if (!values.TryGetValue(key, out value))
                {
                    value = string.Format("{{{0}}}", key);
                }
                array[i] = value;
            }
            return string.Format(_templateWithIndexes, array);
        }
    
        private IDictionary<string, object> MakeDictionary(object obj)
        {
            Dictionary<string, object> dict = new Dictionary<string, object>();
            Type type = obj.GetType();
            foreach (string propName in _placeholders)
            {
                var prop = type.GetProperty(propName);
                if (prop != null)
                    dict.Add(propName, prop.GetValue(obj, null));
            }
            return dict;
        }
    
        public string Format(object values)
        {
            return Format(MakeDictionary(values));
        }
    
        public static string Format(string template, IDictionary<string, object> values)
        {
            return new StringTemplate(template).Format(values);
        }
    
        public static string Format(string template, object values)
        {
            return new StringTemplate(template).Format(values);
        }
    }
    
  2. Elliott

    • 2020/6/20

    The autocapitalize global attribute is an enumerated attribute that controls whether and how text input is automatically capitalized as it is 

  3. Ira

    • 2018/2/2

  4. Atlas

    • 2021/8/8

    Using this attribute/tag we can transform text with out really editing the text It takes the following values. a)capitalize : This will make the first letter of the every word as a capital letter. b)uppercase : It will show all the letters as uppercase letters. c)lowercase : It will show all the letters as lowercase letters.

  5. Lambert

    • 2020/2/7

    Looping through all properties using reflection and replacing the keys in the string would look roughly like this:

    var args = new object[0];
    foreach(var prop in typeof(User).GetProperties()) {
      if (prop.CanRead) {
        string val = prop.GetGetMethod().Invoke(user, args).ToString();
        sb.Replace("${user." + prop.Name +"}", val);
      }
    }
    

    It uses CanRead to check if the property has getter and then invokes the getter to read the value. A value is converted to string simply using ToString which will probably work for primitive types (depending on the required behavior). This is case sensitive, so you may want to use ToLower if the users write keys using lowercase (as in your example).

  6. Scott

    • 2016/9/2

    leaves the text's case and capitalization exactly as it was entered.

  7. Romeo

    • 2020/7/5

    HTML & CSS. css3, general-dev. mawburn. December 12, 2014, 3 But if I have to, yes, I do use camel case. 2 Likes. system closed April 14, 2015, 6:08am #11. This topic was automatically closed

  8. Murati

    • 2020/1/12

    www.w3schools.com › cssref › css3_pr_transform

  9. Marcel

    • 2018/8/10

    You can loop through the properties by calling typeof(User).GetProperties().

  10. Oakley

    • 2019/8/23

    capitalize only affects the first letters of words. It will not change the case of the rest of the letters in a word. For example, if you capitalize a word that's in all capital 

  11. Bernardi

    • 2017/5/8

    The :first-letter pseudo-element targets the first letter of your element, not the first letter of each word. Besides, you're wrappring your code in a span in HTML and targetting a th in CSS, is it supposed to be like that? Try using this instead :.listing-table table th{ text-transform: capitalize; } Documentation of text-transform 

  12. Eddie

    • 2021/3/18

    ::first-letter is a pseudo-element which selects the first letter in the first line of Only a subset of CSS properties can be used to style ::first-letter 

  13. Baker

    • 2019/6/24

    HTML Tags <!--> <!DOCTYPE> <a> <input> element with type="password" that must contain 8 or more characters that are of at least one number, and one uppercase and

  14. Alberto

    • 2019/5/19

    and press SHIFT + F3 until the case you want is applied.

  15. Turner

    • 2019/4/12

    Interesting question! capitalize transforms every first letter of a word to uppercase, but it does not transform the other letters to lowercase. Not even the :first-letter pseudo-class will cut it (because it applies to the first letter of each element, not each word), and I can't see a way of combining lowercase and capitalize to get the desired outcome.

  16. Tony

    • 2020/12/3

    help improve legibility for ruby.

  17. Silvestri

    • 2015/4/17

    If you are facing that text with the CSS property text-transform: capitalize; doesn't work, please continue reading to get how to fix it.. How to fix text-transform. When I faced this issue, it was due to all caps text, so you need to transform it to lowercase in your template engine.

  18. Jacoby

    • 2018/12/22

    translate() : Moves an element sideways or up and down. rotate() : Rotates the element clockwise from its current position. matrix() : A function that 

  19. Cason

    • 2019/4/16

    capitalize only affects the first letters of words. It will not change the case of the rest of the letters in a word. For example, if you capitalize a word that's in all capital 

  20. Neri

    • 2019/10/7

    initial-letter is a CSS property that selects the first letter of the element where it is applied and specifies the number of lines the letter occupies. You may have seen something like this on news sites, where the first letter of a leading paragraph is larger than the rest of the content. The New Yorker’s site styles the initial letter

  21. Oliver

    • 2021/3/23

    -Transform Values

  22. Yael

    • 2016/4/20

    Question is in the title. I'm used to using camelcase in development and, since there are crossovers with design, was wondering if there are any technical reasons (regardless of opinions) against using it in HTML. Thanks in advance for your replies.

  23. Wade

    • 2017/1/27

    www.w3schools.com › css › css3_2dtransforms

  24. Creed

    • 2018/8/15

    toUpperCase() to your header strings. Additionally, it's not uncommon to only want the first letter of a word to be capitalized. JavaScript doesn't have a capitalize 

  25. Trey

    • 2016/6/29

    To capitalize each character by using CSS, at first you need to take a detailed look towards the text transformation property. The values of text transformation property are mentioned below. None: It does mean that no capitalization. Capitalize: It does mean that the first characters of each word will be capitalized.

  26. Ace

    • 2020/2/17

    Transforms open up a world of opportunity with HTML and CSS, learn how to use them to The syntax is very similar to that of the scale and translate values.

  27. Israel

    • 2015/4/27

    .

  28. Forest

    • 2018/7/16

    Bootstrap CSS class text-capitalize with source code and live preview. You can copy our examples and paste them into your project! Use 230+ ready-made Bootstrap components from the multipurpose library.

  29. Lucca

    • 2020/1/14

    There is no css property for camel case. Capitalize will make the first letter of each word uppercase, but there is nothing that I know of in css that 

  30. King

    • 2016/8/22

    Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

  31. Adonis

    • 2021/3/26

    CSS1

  32. Cairo

    • 2019/11/25

    Google Chrome - CSS text-transform not working. Ask Question Asked 9 years ago. Active 8 years, 8 months ago. Viewed 14k times 3. So I have this code that works in

  33. Rudy

    • 2018/2/4

    In CSS, initial refers to the initial value of the element. Since the initial value of elements for text-transform is none , using initial will have the same effect as using 

  34. Alessandro

    • 2015/8/7

  35. Avery

    • 2019/8/21

    For other input types, there are attributes available that do what they say: <input type="text" autocorrect="off" autocapitalize="none">. If for some 

Comments are closed.

More Posts