skip to Main Content

I have a Comma separated Upper case string with spaces that needs to be converted into a comma separated string in Title Case and trim the end spaces only.

Sample String:

"BAHAMAS, BAHRAIN, BANGLADESH, BONAIRE SINT EUSTATIUS, BOSNIA HERZEGOVINA"

Desired String:

"Bahamas,Bahrain,Bangladesh,Bonaire Sint Eustatius,Bosnia Herzegovina"

The code that I tried:

string s_Response = responseString.Split(',')
                                .Select(x=>x.Trim())
                                .Select(y=>y.Join(" ",y.Split(' ').Select(i=>i.Substring(0,1).ToUpper()+i.Substring(1).ToLower()).ToArray()))
       

This however gives me an error for Join (qualify it with a type name instead)

I seek help for setting the logic of array comparisons

Any help is appreciated 🙂

2

Answers


  1. TextInfo.ToTitleCase(String) Method – Converts the specified string to title case (except for words that are entirely in uppercase, which are considered to be acronyms). String.Replace will help with spaces:

    string s_Response = (new CultureInfo("en-US",false).TextInfo
        .ToTitleCase(responseString.Replace(", ", ","));
    
    Login or Signup to reply.
  2. When all else fails, there’s always a good ol’ fashioned state machine:

    public const string Input = "BAHAMAS, BAHRAIN, BANGLADESH, BONAIRE SINT EUSTATIUS, BOSNIA HERZEGOVINA";
    
    public static string DoCapitalize (string input)
    {
        TheState theState = TheState.Beginning;
        CharacterState characterState;
        var buffer = new StringBuilder (input.Length);
    
        foreach (char c in input)
        {
            characterState = GetCharacterState(c);
            switch (theState)
            {
                case TheState.Beginning:
                    if (characterState == CharacterState.Letter)
                    {
                        buffer.Append(char.ToUpper(c));
                        theState = TheState.InWord;
                    }
                    break;
                case TheState.InWord:
                    if (characterState == CharacterState.Letter)
                    {
                        buffer.Append(char.ToLower(c));
                    }
                    else if (characterState == CharacterState.Comma)
                    {
                        buffer.Append(c);
                        theState = TheState.BetweenNames;
                    }
                    else if (characterState == CharacterState.Space)
                    {
                        buffer.Append(c);
                        theState = TheState.BetweenWords;
                    }
                    break;
                case TheState.BetweenNames:
                    if (characterState == CharacterState.Letter)
                    {
                        buffer.Append(char.ToUpper(c));
                        theState = TheState.InWord;
                    }
                    break;
                case TheState.BetweenWords:
                    if (characterState == CharacterState.Letter)
                    {
                        buffer.Append(char.ToUpper(c));
                        theState = TheState.InWord;
                    }
                    break;
            }
        }
        return buffer.ToString ();
    
    
        static CharacterState GetCharacterState(char c)
        {
            CharacterState characterState;
            if (c == ' ')
            {
                characterState = CharacterState.Space;
            }
            else if (c == ',')
            {
                characterState = CharacterState.Comma;
            }
            else if (char.IsLetter(c))
            {
                characterState = CharacterState.Letter;
            }
            else
            {
                characterState = CharacterState.Other;
            }
    
            return characterState;
        }
    }
    

    Note that the GetCharacterState function is a local function (it gets it out of the main program flow, but keeps it with the main function.

    If you want to have a list of "noise works" (of, the, etc.), you could easily implement it. The code is not simple, but it is pretty easy to read, and it is O(N).

    Test output from the stated input:
    Bahamas,Bahrain,Bangladesh,Bonaire Sint Eustatius,Bosnia Herzegovina

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search