-1

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
  • 1
    This is likely a much bigger problem than you realize. Not everything is always capitalized in real title case ("of", "a", "an", "the", "etc" are often left unchanged), certain other things (acronyms) should remain all caps, and getting a computer to know the difference turns out to be almost as much work as teaching a computer the entire English language. Now often we get lucky, and we can narrow this down to certain kinds of input... just be aware which option you've signed up for. Commented Sep 21, 2023 at 20:46
  • 1
    instead of y.Join you should try string.Join. Commented Sep 21, 2023 at 20:57

3 Answers 3

2

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(", ", ","));
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

It's much easier to reason about it when decomposing each step:

using System;
using System.Linq;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        string input = "BAHAMAS, BAHRAIN, BANGLADESH, BONAIRE SINT EUSTATIUS, BOSNIA HERZEGOVINA";  
        Console.WriteLine(input);
            
        string output = FormatCountries(input); 
        Console.WriteLine(output);
    }
    
    private static string FormatCountries(string input)
    {   
        StringBuilder output = new StringBuilder();
    
        foreach(string roughCountry in input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            string cleanedCountry = roughCountry.Trim().ToLower();
        
            string[] countryIndividualWords = cleanedCountry.Split(new string[] { " " }, StringSplitOptions.None);
        
            IEnumerable<string> individualFormatedWords = countryIndividualWords.Select(x => x.First().ToString().ToUpper() + x.Substring(1));
            string formatedCountry = string.Join(" ", individualFormatedWords);
        
            output.Append(formatedCountry + ",");
        }
    
        return output.Remove(output.Length - 1, 1).ToString(); // Remove the last comma
    }  
}

Here is a .NET Fiddle if you want to check it out.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.