-4

I am a new developer and this is the first time that I have worked with a winform and in addition I am taking over someone else's project, so there are things that I do not fully understand.

I have an "Item to modify" dropdown displayed and this list was made this way: "public DisplayItem HorseName {get; set;}", so the terms in my dropdown are displayed like this and I would like to divide them into 2 when there is an uppercase example: Horse Name. I don't know if I should do a Split or a Regex.Replace and especially where to do it since it's a list of "item to modify" which goes through 2-3 places. My guest would be after the k.Name in the request, but it didn't work.

As I tell you the starting code is not mine, so the place where to place it leaves me perplexed

This is my code:

[NonSerialized]
        public static Dictionary<string, PropertyInfo> DisplayItemProperties;

    static DisplaySettings()
    {
        DisplayItemProperties = (from p in typeof(DisplaySettings).GetProperties()
                                 where p.PropertyType == typeof(DisplayItem)
                                 select p).ToDictionary(k => k.Name, v => v);
    }

    public IEnumerable<DisplayItem> DisplayItems()
    {
        return DisplayItemProperties.Select(item => item.Value.GetValue(this, null)).Cast<DisplayItem>();
    }

this.cbDisplaySetting.Items.AddRange(DisplaySettings.DisplayItemProperties.Keys.Cast().ToArray());

SOLUTION:

This:

this.cbDisplaySetting.Items.AddRange(DisplaySettings.DisplayItemProperties.Keys.Cast<object>().ToArray());

became:

        List<string> itemsLst = DisplaySettings.DisplayItemProperties.Keys.ToList<string>();
        for(var i = 0; i < itemsLst.Count; i++)
        {
            itemsLst[i] = Regex.Replace(itemsLst[i], "(\\B[A-Z])", " $1");
        }

        this.cbDisplaySetting.Items.AddRange(itemsLst.Cast<object>().ToArray());
  • Where's you attempt? – Aluan Haddad Sep 08 '20 at 17:41
  • Try following : string input = "HelloWorld"; string pattern = "(?'first'[A-Z][a-z]+)(?'second'[A-Z][a-z]+)"; string output = Regex.Replace(input, pattern, "${first} ${second}"); – jdweng Sep 08 '20 at 17:44
  • @AluanHaddad I edit my post – Frankthetank Sep 08 '20 at 17:46
  • None of the code you posted includes a call to `Split()`, never mind is it a useful [mcve]. That said, if you want to use a regular expression to split the string, why are you calling `string.Split()` instead of [Regex.Split()](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.split)? Please improve the question. – Peter Duniho Sep 08 '20 at 17:53
  • See e.g. https://stackoverflow.com/questions/7825496/regex-split-a-string – Peter Duniho Sep 08 '20 at 17:54

1 Answers1

0

Is Regex required? Here's linq.

edit: added a regex option as well since that was requested.

([a-z])([A-Z])", "$1 $2") matches lowercase letter and then uppercase letter and returns the matches as $1 and $2 respectively w/ a space in between.

Note: this won't work if you have accented characters like É, is that needed in your case?

    string input = "HelloWorldHowAreYouToday";
    string output = string.Concat(input.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');
    Console.WriteLine(output);
    
    //regex example.
    string regExResult = Regex.Replace(input, @"([a-z])([A-Z])", "$1 $2");
    Console.WriteLine(regExResult);

output for both: Hello World How Are You Today

Mikael
  • 1,002
  • 1
  • 11
  • 22