gpt4 book ai didi

c# - 如何将每个句子的第一个字符大写

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:51 24 4
gpt4 key购买 nike

使用接受字符串作为参数的方法创建一个应用程序,并返回字符串的副本,每个句子的第一个字符大写。

这是我到目前为止所要做的,但我似乎做对了:

    //Create method to process string.
private string Sentences(string input)
{
//Capitalize first letter of input.
char firstLetter = char.ToUpper(input[0]);

//Combine the capitalize letter with the rest of the input.
input = firstLetter.ToString() + input.Substring(1);

//Create a char array to hold all characters in input.
char[] letters = new char[input.Length];

//Read the characters from input into the array.
for (int i = 0; i < input.Length; i++)
{
letters[i] = input[i];
}

//Loop through array to test for punctuation and capitalize a character 2 index away.
for (int index = 0; index < letters.Length; index++)
{
if(char.IsPunctuation(letters[index]))
{
if (!((index + 2) >= letters.Length))
{
char.ToUpper(letters[index+ 2]);
}
}
}

for(int ind = 0; ind < letters.Length; ind++)
{
input += letters[ind].ToString();
}

return input;
}

最佳答案

您可以使用 Linq.Aggregate n - 查看代码和代码输出中的注释以了解其工作原理。

这个也符合“Bla.blubb”——你需要检查“.?!”之后的空格

using System;
using System.Linq;

internal class Program
{
static string Capitalize(string oldSentence )
{
return

// this will look at oldSentence char for char, we start with a
// new string "" (the accumulator, short acc)
// and inspect each char c of oldSentence

// comment all the Console.Writelines in this function, thats
// just so you see whats done by Aggregate, not needed for it to
// work

oldSentence
.Aggregate("", (acc, c) =>
{
System.Console.WriteLine("Accumulated: " + acc);
System.Console.WriteLine("Cecking: " + c);

// if the accumulator is empty or the last character of
// trimmed acc is a ".?!" we append the
// upper case of c to it
if (acc.Length == 0 || ".?!".Any(p => p == acc.Trim().Last())) // (*)
acc += char.ToUpper(c);
else
acc += c; // else we add it unmodified

System.Console.WriteLine($"After: {acc}\n");

return acc; // this returns the acc for the next iteration/next c
});
}

static void Main(string[] args)
{
Console.SetBufferSize(120, 1000);
var oldSentence = "This is a testSentence. some occurences "
+ "need capitalization! for examlpe here. or here? maybe "
+ "yes, maybe not.";

var newSentence = Capitalize(oldSentence);

Console.WriteLine(new string('*', 80));
Console.WriteLine(newSentence);
Console.ReadLine();
}
}

(*)

  • ".?!".Any(p => p == ... )) 表示字符串 ".?!" 包含等于的任何字符...
  • acc.Trim().Last() 表示:删除 acc 前端/末尾的空格并给我最后一个字符

.Last().Any() 也是 Linq。大多数 Linq-esc 扩展都可以在这里找到: https://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.110).aspx

输出(被截断 - 它相当长;o)

Accumulated:
Cecking: T
After: T

Accumulated: T
Cecking: h
After: Th

Accumulated: Th
Cecking: i
After: Thi

Accumulated: Thi
Cecking: s
After: This

Accumulated: This
Cecking:
After: This

Accumulated: This
Cecking: i
After: This i

Accumulated: This i
Cecking: s
After: This is

<snipp - .. you get the idea how Aggregate works ...>

Accumulated: This is a testSentence.
Cecking: s
After: This is a testSentence. S

<snipp>

Accumulated: This is a testSentence. Some occurences need capitalization!
Cecking: f
After: This is a testSentence. Some occurences need capitalization! F

<snipp>
********************************************************************************
This is a testSentence. Some occurences need capitalization! For examlpe here. Or here? Maybe yes, maybe not.

关于c# - 如何将每个句子的第一个字符大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47337011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com