首页 > 解决方案 > 将句子开头的每个单词大写。一个字符串中的多个句子。视觉 C#

问题描述

所以,我正在尝试处理用户输入到文本框中的字符串,这样如果用户忘记在句子开头大写单词,我的程序就会这样做。我一直在努力处理字符串和子字符串并修改它们。我似乎无法很好地掌握它。我可以弄清楚如何获取字符串第一句中第一个单词的第一个字母,或者使字符串的所有字母大写,但之后我迷路了,因为我不知道如何向下工作用于查找第一个单词字母没有大写的下一个句子的字符串。我会发布我的代码,但它可能没有任何用处。感谢您抽出时间提前提供帮助。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Chapter8_Problem3_
{
    public partial class Form1 : Form
    {
        // variable to hold users sentence from the text box
        private string userSentence;
  
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // call capitalize method
            capString(userSentence);
        }

        // capitalize method
        private void capString(string userSentence)
        {
            // assign the user sentence variable the text box the user enters a string in
            userSentence = inputTextBox.Text;
            
            // if nothing in text box, print message
            if (userSentence.Length == 0)
            {
                MessageBox.Show("There was no sentence entered in the text box");
            }
            
           // capitalize words needed
           
            if(char.IsLower(userSentence[0]))
            {
                char c = char.ToUpper(userSentence[0]);
                listBox1.Items.Add(userSentence.ToUpper());
            }
            



           
            
            
           
            
        }
    }
}

标签: c#stringsubstring

解决方案


第一关

我的第一个想法如下。它适用于给定的输入。

输入:

string textIn = @"first sentence. second sentence. third sentence.";

在一行代码中拆分句点、修剪、应用句子大小写和重组:

string textOut = string.Join(". ", 
    textIn.Split(new char[] { '.' })
        .Select(x => x.Trim())
        .Where(x => x.Length > 0)
        .Select(x => x.Substring(0,1).ToUpper() + x.Substring(1))) 
    + ".";

Console.WriteLine(textOut);

输出:

First sentence. Second sentence. Third sentence.

string.split() 函数将字符串拆分为四个组件,最后一个组件为空。这就是为什么我测试每个组件的长度并跳过空的。你可能想也可能不想这样做。

第二遍

然后我看到了上面关于缩写的评论,所以我用这个字符串尝试了一下。

string text = @"first sentence. i was born in the U.S.A. third sentence.";

输出是这个

First sentence. I was born in the U. S. A. Third sentence.

看起来仍然不错,但是在美国的前两个时期之后插入了空格 这可能会也可能不会接受。

string.split() 函数将字符串拆分为一个或多个单个字符。我还尝试使用正则表达式将字符串拆分为句点、问号和感叹号,后跟一个或多个空格字符。

string textIn = @"first sentence. i was born in the U.S.A. third sentence.";

string[] sentences = Regex.Split(textIn, @"\.\s+");

string textOut = string.Join(". ",
    sentences
        .Select(x => x.Trim())
        .Where(x => x.Length > 0)
        .Select(x => x.Substring(0, 1).ToUpper() + x.Substring(1)))
    + ".";

Console.WriteLine(textOut);

输出

First sentence. I was born in the U.S.A. Third sentence.

正则表达式正确地忽略了 USA 的前两个句点(因为它们后面没有空格)并且出于同样的原因不匹配最后一个句点。

第三关

另一种选择是使用有限状态机

此示例说明如何将句子的第一个字符大写,其中句子定义为字符串,后跟句号、问号或感叹号,后跟空格。

在此示例中,状态由整数“状态”表示。

定义了三种状态: 0 = 搜索下一个句子的第一个字符。1 = 搜索下一个分隔符。2 = 检查分隔符后的第一个字符以确定我们是否到达了句子的结尾。

有两种操作:(1) 将当前字符大写,(2) 将当前字符附加到输出中。

字符串是逐个字符解析的。执行的动作和状态转换由当前状态和当前角色决定。

    static string CapitalizeSentencesInString(string textIn) {

        string textOut = "";

        // Delimiters: dot, hook, & bang.
        char[] delimiters = new char[] { '.', '?', '!' };

        int state = 0;
        
        foreach(char ch in textIn) {

            switch (state) {

                // Searching for first character of the next sentence.
                case 0:

                    //  Space character. 
                    if (ch == ' ') {
                        
                        //  Action: append character to output.
                        textOut += ch;
                        
                        //  Next state: keep searching for the first 
                        //  character of the next sentence (i.e., do 
                        //  not change state).

                    //  Dot, hook, or bang.
                    } else if (delimiters.Contains(ch)) {
                        
                        //  Action: append to output.
                        textOut += ch;
                        
                        //  Next state: check next character.
                        state = 2;

                    //  Upper case character.
                    } else if (char.IsUpper(ch)) {

                        //  Action: append to output.
                        textOut += ch;

                        //  Next state: search for the end of the 
                        //  current sentence.
                        state = 1;

                    // Lower case character.
                    } else if (char.IsLower(ch)) {

                        //  Action: convert to upper case and append to 
                        //  output.
                        textOut += char.ToUpper(ch);

                        //  Next state: search for the end of the 
                        //  current sentence.
                        state = 1;

                        // Default option.
                    } else {

                        //  Action: append to output.
                        textOut += ch;

                        //  Next state: search for the end of the 
                        //  current sentence.
                        state = 1;
                    }

                    break;

                // Searching for next delimiter.
                case 1:

                    // Dot, hook, or bang.
                    if (delimiters.Contains(ch)) {

                        //  Action: append to output.
                        textOut += ch;

                        //  Next state: check next character.
                        state = 2;

                    } else {

                        //  Action: append to output.
                        textOut += ch;

                        //  Next state: keep searching for the next 
                        //  delimiter. (i.e., do not change state).

                    }

                    break;

                //  Previous character was a delimiter. This character 
                //  determines whether we have reached the end of the sentence.
                case 2:

                    //  Space. We have reached the end of the sentence.
                    if (ch == ' ') {

                        //  Action: append to output.
                        textOut += ch;

                        //  Next state: search for the first character of 
                        //  the next sentence.
                        state = 0;

                    // Dot, hook, or bang.
                    }  else if (delimiters.Contains(ch)) {

                        //  Action: append to output.
                        textOut += ch;

                        //  Next state: check next character  
                        //  (i.e., do not change state).

                    //  Not a space. We have not reached the end of 
                    //  the sentence.
                    } else {

                        //  Action: append to output.
                        textOut += ch;

                        //  Next state: search for the next delimiter.
                        state = 1;

                    }

                    break;
            }
        }

        return textOut;
    }

输入:

string textIn = @"first sentence! can i also a handle ellipses...? i was born in the U.S.A. third sentence.";

输出:

First sentence! Can i also a handle ellipses...? I was born in the U.S.A. Third sentence.

看起来不错,但是这个呢?

string text = @"i was born in the U.K. but I live in the U.S.A.. Third sentence."

输出:

First sentence! i was born in the U.K. But I live in the U.S.A.. Third sentence.

哦!'but'中的'b'大写!

结论

这是一个不平凡的问题。您可以尝试几种不同的方法。你的成功将取决于你对“句末”的定义有多严格,以及你能否将它与句中句号的其他用法区分开来。您可以很容易地处理绝大多数情况,但总是有可能意外的边缘情况让您绊倒。


推荐阅读