C# 如何大写每个句子的第一个字符
使用一个方法创建一个应用程序,该方法接受字符串作为参数,并返回每个句子的第一个字符大写的字符串副本 这就是我目前所要做的,我似乎做得不对:C# 如何大写每个句子的第一个字符,c#,C#,使用一个方法创建一个应用程序,该方法接受字符串作为参数,并返回每个句子的第一个字符大写的字符串副本 这就是我目前所要做的,我似乎做得不对: //Create method to process string. private string Sentences(string input) { //Capitalize first letter of input. char firstLetter = char.ToUpper(input[0]
//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;
}
//创建处理字符串的方法。
私有字符串语句(字符串输入)
{
//将输入的第一个字母大写。
char firstLetter=char.ToUpper(输入[0]);
//将大写字母与输入的其余部分合并。
input=firstLetter.ToString()+input.Substring(1);
//创建一个字符数组以保存输入中的所有字符。
char[]字母=新字符[input.Length];
//将输入中的字符读入数组。
for(int i=0;i=字母长度))
{
字符ToUpper(字母[索引+2]);
}
}
}
for(int ind=0;ind
我建议使用正则表达式来识别句子中的所有点。获取匹配项,使其大写,并将其替换回原始句子中的匹配索引中。我现在没有任何IDE可以在.NET上尝试代码,但为了更好地理解,我可以用伪代码编写它
String setence = "your.setence.goes.here";
Regex rx = new Regex("/\..*?[A-Z]/");
foreach (Match match in rx.Matches(sentence))
{
setence.remove(match.Index, 2).insert(match.Index, String.ToUpper(match.Value));
}
您可以使用
Linq.Aggregate
n-请参阅代码和代码输出中的注释以了解其工作原理
这一个将尊重“Bla.blubb”以及-你需要检查后的空白“?”
(*)
表示字符串“?”。Any(p=>p==…)
是否包含任何等于“?”
…
的意思是:删除acc.Trim().Last()
前面/后面的空格,并给我最后一个字符acc
.Last()
和.Any()
也是Linq。大多数Linq esc扩展都可以在这里找到:
输出(剪断-相当长;o)
累计:
切金:T
之后:T
累计:T
切金:h
之后:Th
累计:次
切金:我
之后:Thi
累积:Thi
塞金:是的
之后:这个
累积:这个
切金:
之后:这个
累积:这个
切金:我
之后:我
这是我
塞金:是的
之后:这是
这是一个测试句。
塞金:是的
这是一个测试句子。s
这是一个测试句。有些事情需要资本化!
塞金:f
这是一个测试句子。有些事情需要资本化!F
********************************************************************************
这是一个测试句。有些事情需要资本化!举个例子。还是在这里?也许是的,也许不是。
您有两项任务:
1) 把文本分成句子
2) 将句子中的第一个字符大写
任务一可能非常复杂,例如,因为有很多疯狂的语言。既然这是家庭作业,我想你们可以继续,简单地用众所周知的分隔符分开
任务二只是关于基本的字符串操作。选择第一个字符,使其大写,并通过子字符串操作添加句子缺少的部分
下面是一个代码示例:
char[] separators = new char[] { '!', '.', '?' };
string[] sentencesArray = "First sentence. second sentence!lastone.".Split(separators, StringSplitOptions.RemoveEmptyEntries);
var i = 0;
Array.ForEach(sentencesArray, e =>
{
sentencesArray[i] = e.Trim().First().ToString().ToUpper() +
e.Trim().Substring(1);
i++;
});
我在Groovy中为同样的问题创建了一个方法
String capitalizeFirstCharInSentence(String str) {
String result = ''
str = str.toLowerCase()
List<String> strings = str.tokenize('.')
strings.each { line ->
StringBuilder builder = new StringBuilder(line)
int i = 0
while (i < builder.size() - 1 && !Character.isLowerCase(builder.charAt(i))) {
i++
}
if (Character.isLowerCase(builder.charAt(i))) {
builder.setCharAt(i, builder.charAt(i).toUpperCase())
result += builder.toString() + '.'
}
}
return result
}
String大写首字母内容(String str){
字符串结果=“”
str=str.toLowerCase()
列表字符串=str.tokenize('.'))
strings.each{line->
StringBuilder=新的StringBuilder(线)
int i=0
而(i
我喜欢您格式化方法的方式,因为它使新的编码人员更容易阅读,所以我决定在维护结构的同时尝试使代码正常工作。我看到的主要问题是在格式化数组后没有替换它们
//Create method to process string.
private string Sentences(string input)
{
//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];
}
//Capitalize first letter of input.
letters[0] = char.ToUpper(letters[0]);
//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)
{
letters[index + 2] = char.ToUpper(letters[index+ 2]);
}
}
}
// convert array back to string
string results = new string(letters)
return results;
}
//创建处理字符串的方法。
私有字符串语句(字符串输入)
{
//创建一个字符数组以保存输入中的所有字符。
char[]字母=新字符[input.Length];
//将输入中的字符读入数组。
for(int i=0;i 如果(索引+2)“我似乎做得不对。”-一些细节会很有用。你的代码有什么问题?我发现你的算法至少有一个概念性问题。如果标点符号后的下一个字母不是两个字符呢?标点符号后可能有两个空格,也可能是新行字符。这听起来像是家庭作业,你知道se的参数吗句子识别?因为这很难。当我必须这样做时,我会将文本发送到Microsoft认知服务,让它为我拆分句子。是的,这是一个家庭作业问题。假设输入是一个包含许多句子的段落。我如何确保每个句子的所有首字母都大写。我尝试了brea
String capitalizeFirstCharInSentence(String str) {
String result = ''
str = str.toLowerCase()
List<String> strings = str.tokenize('.')
strings.each { line ->
StringBuilder builder = new StringBuilder(line)
int i = 0
while (i < builder.size() - 1 && !Character.isLowerCase(builder.charAt(i))) {
i++
}
if (Character.isLowerCase(builder.charAt(i))) {
builder.setCharAt(i, builder.charAt(i).toUpperCase())
result += builder.toString() + '.'
}
}
return result
}
//Create method to process string.
private string Sentences(string input)
{
//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];
}
//Capitalize first letter of input.
letters[0] = char.ToUpper(letters[0]);
//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)
{
letters[index + 2] = char.ToUpper(letters[index+ 2]);
}
}
}
// convert array back to string
string results = new string(letters)
return results;
}