PaddiM8
PaddiM8

Reputation: 175

C# Capitalize first letter in in each sentence in a string

I want to capitalize the first letter of each sentence in a string. I have a string, ex. "hello, how are you? i'm fine, you? i'm good. nice weather!"

and I want to capitalize the first letter of each sentence in it. So, "Hello, how are you? I'm fine, you?" etc.

EDIT: So far, I've just tried

public static string FirstCharToUpper(string input)
        {
            if (String.IsNullOrEmpty(input))
                throw new ArgumentException("ARGH!");
            return input.First().ToString().ToUpper() + input.Substring(1);
        }

but this capitalizes the first letter in each word, not sentence :/

Upvotes: 3

Views: 10557

Answers (5)

Dusten Salinas
Dusten Salinas

Reputation: 1

It's been stated a few times, iterate over the character array. The primary reason for this is it keeps the string structure vs say a split, toupper/substring, and join.

Below handles a few scenarios, such as don't capitalize a letter after a digit. It also makes some assumptions in addition to those described/solved for above, hope it helps.

public static string CapitalizeFirstLetterInEachSentence(string sentences, bool capitalizeFirstSentence = true)
{
  bool capitalizeNextLetterOrDigit = capitalizeFirstSentence;
  char[] characters = sentences.ToCharArray();
  var len = characters.Length;

  for (int i = 0; i < len; i++)
  {
    var character = characters[i];
    switch (character)
    {
      case '.':
      case '?':
      case '!':
        capitalizeNextLetterOrDigit = true;
        continue;
    }
    
    if (capitalizeNextLetterOrDigit && char.IsLetterOrDigit(character))
    {
      capitalizeNextLetterOrDigit = false;
      characters[i] = char.ToUpper(character);
    }
  }
  return new string(characters);
}

Upvotes: 0

Valentin
Valentin

Reputation: 5488

I suggest simple method, that iterates over the string.

You can also make it as an extension for a string.

public static class StringExtension
{
    public static string CapitalizeFirst(this string s)
    {
        bool IsNewSentense = true;
        var result = new StringBuilder(s.Length);
        for (int i = 0; i < s.Length; i++)
        {
            if (IsNewSentense && char.IsLetter(s[i]))
            {
                result.Append (char.ToUpper (s[i]));
                IsNewSentense = false;
            }
            else
                result.Append (s[i]);

            if (s[i] == '!' || s[i] == '?' || s[i] == '.')
            {
                IsNewSentense = true;
            }
        }

        return result.ToString();
    }
}

So, you can use it following way

 string str = "hello, how are you? i'm fine, you? i'm good. nice weather!".CapitalizeFirst();

So str equals to

Hello, how are you? I'm fine, you? I'm good. Nice weather!

Upvotes: 7

C&#233;sar Caldeira
C&#233;sar Caldeira

Reputation: 1

You will need a text box called txtInput and another called txtOutput. Then run this code, e.g. upon a button press:

 bool Upper = true;
        foreach(char c in txtInput.Text)
        {
            if (Upper == true)
            {
                if (c == ' ')
                {
                    txtOutput.Text += c;
                    continue;
                }
                txtOutput.Text += c.ToString().ToUpper();
                Upper = false;
            }
            else
            {
                txtOutput.Text += c;
            }
            if (c == '?' || c == '!' || c == '.')
                Upper = true;


        }

Screen shot:

screenshot

Upvotes: 0

Vivek Shukla
Vivek Shukla

Reputation: 733

Microsoft Outlook 2010 provides an option to format the selected text to a sentence case, please go to Format text Tab - Font group, you would find a change case option beside shrink font option, cutting long story short, you need something like that in your code to happen, unfortunately there is no built in property defined in .net.

However please have a look a the conversation already happened in stack overflow. .NET method to convert a string to sentence case

Upvotes: 0

stuck
stuck

Reputation: 1570

Iterate over string: Create a list which holds stop characters.

Then check every letter in that string, if it is in the list or not. If it is in the list, then capitalize the character after that.

For first character, it's always uppercase, you should do it static.

Or, you can do it like Dan's said, it works too.

Upvotes: 0

Related Questions