简体   繁体   English

C# 将字符串中每个句子的首字母大写

[英]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 :/但这将每个单词的第一个字母大写,而不是句子:/

I suggest simple method, that iterates over the string. 我建议简单的方法,迭代字符串。

You can also make it as an extension for a string . 您也可以将其作为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 所以str等于

Hello, how are you? 你好,你好吗? I'm fine, you? 我很好,你呢? I'm good. 我很好。 Nice weather! 好天气!

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. 或者,你可以像丹说的那样做,它也有效。

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. Microsoft Outlook 2010提供了将所选文本格式化为句子大小写的选项,请转到格式化文本选项卡 - 字体组,您会在缩小字体选项旁边找到一个更改大小写选项,缩短很长一段时间,你需要这样的东西在你的代码发生,遗憾的是.net中没有定义内置属性。

However please have a look a the conversation already happened in stack overflow. 但请查看堆栈溢出中已经发生的对话。 .NET method to convert a string to sentence case .NET方法将字符串转换为句子大小写

You will need a text box called txtInput and another called txtOutput . 您将需要一个名为txtInput的文本框和另一个名为txtOutput的文本框。 Then run this code, eg 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: 截屏:

截图

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用char将C#中每个句子的第一个字母大写? - How can I capitalize the first letter of each sentence in C# using char? 如何把每个句子的首字母大写? - How to capitalize first letter of each sentence? 如何将每个句子的首字母大写,并给用户将字母更改为较低的机会? - How to capitalize first letter of each sentence AND give User the chance to change the letter to lower? C#-如何在段落中将首字母“大写”大写? (与正则表达式结合使用) - C# - How to capitalize first letter 'only' on a paragraph? (Combined with regex) 如何使用 C# 将每个单词的第一个字符或整个字符串的第一个字符大写? - How to capitalize the first character of each word, or the first character of a whole string, with C#? 如何在 C# 中将字符串的每三个字母大写? - How can I capitalize every third letter of a string in C#? 如何在C#中将大写的名字和姓氏大写? - How do I capitalize first letter of first name and last name in C#? 如何使用XAML和C#在WP8中实现转换器以大写TexBlock中单词的首字母? - How to implement a converter in WP8 using XAML and C# for capitalize the first letter of a word in a TexBlock? 如何使用 c# 的按键事件将每个单词的首字母大写? - How can i capitalize first letter of every word using keypress event with c#? 字符串C#中的首字母大写 - First letter capital in string c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM