[英]printing a reverse sentence with punctuation c#
我正在嘗試顛倒如下句子:輸入:
我的名字是約翰。 我23歲。
output:
.old years 23 am i.john 是我的名字
我無法弄清楚如何在最后切換點。 我嘗試使用Split
,但它總是返回單詞末尾的點。
string[] words = sentence.Split(' ');
Array.Reverse(words);
return string.Join(" ", words);
在單詞開始之前添加額外的邏輯來移動句點('.'),例如
var sentence ="my name is john. i am 23 years old."; //Input string
string[] words = sentence.Split(' '); //Split sentence into words
Array.Reverse(words); //Revere the array of words
//If word starts with . then start your word with period and trim end.
var result = words.Select(x => x.EndsWith('.') ? $".{x.Trim('.')}" : x);
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^This was missing
Console.WriteLine(string.Join(" ", result));
您可以嘗試Linq和正則表達式的組合:
using System.Linq;
using System.Text.RegularExpressions;
...
string source = "my name is john. i am 23 years old.";
// .old years 23 am i .john is name my
string result = string.Join(" ", Regex
.Matches(source, @"\S+")
.Cast<Match>()
.Select(m => Regex.Replace(m.Value, @"^(.*?)(\p{P}+)$", "$2$1"))
.Reverse());
這里我們使用兩種模式:一種簡單的"\S+"
,它匹配任何不是空格的字符。 下一個模式^(.*?)(\p{P}+)$
值得解釋:
^(.*?)(\p{P}+)$
這里
^ - anchor, start of the string
(.*?) - group #1: any symbols, but as few as possible
(\p{P}+) - group #2: one or more punctuation symbols
$ - anchor, end of the string
當匹配時,我們交換這些組: "&2&1"
演示:
private static string Solve(string source) => string.Join(" ", Regex
.Matches(source, @"\S+")
.Cast<Match>()
.Select(m => Regex.Replace(m.Value, @"^(.*?)(\p{P}+)$", "$2$1"))
.Reverse());
...
string[] tests = new string[] {
"my name is john. i am 23 years old.",
"It's me, here am I!",
"Test...",
};
string report = string.Join(Environment.NewLine, tests
.Select(test => $"{test,-35} => {Solve(test)}"));
Console.Write(report);
結果:
my name is john. i am 23 years old. => .old years 23 am i .john is name my
It's me, here am I! => !I am here ,me It's
Test... => ...Test
您正在反轉由空格分隔的單詞(在old.
點是單詞的一部分)。 如果您也想反轉這些點,您想將它們視為單詞(由空格分隔):
public static class TextExtensions
{
public static string PointTrick(this string str) => str.Replace(".", " .");
public static string PointUntrick(this string str) => str.Replace(". ", ".");
public static string ReverseWords(this string str) => string.Join(" ", str.Split(" ").Reverse());
}
這些測試通過了。
[TestClass]
public class SOTests
{
private string GetReversedWithPointTrick(string input) => input.PointTrick().ReverseWords().PointUntrick();
[TestMethod]
public void ReverseWordsTest()
{
var sut = "three two. one";
var expected = "one two. three";
var result = sut.ReverseWords();
Assert.AreEqual(expected, result);
}
[TestMethod]
public void ReverseWords_PointTrick_Test()
{
var sut = "my name is john. i am 23 years old.";
var expected = ".old years 23 am i .john is name my";
var result = GetReversedWithPointTrick(sut);
Assert.AreEqual(expected, result);
}
}
因為總有人要貼出LINQ版的這些東西
sentence.Split().Reverse.Select(w => w[^1] == '.' ? ('.' + w[..^1]) : w);
沒有任何參數的拆分在空格上拆分,Reverse 是一個 LINQ 事物,它反轉輸入,然后我們只有一些邏輯詢問最后一個(^1 是否來自 c# 9 的索引和范圍功能,意思是“來自end") char 是一個點,將它移到開頭(連接一個點加上從末尾到 1 的所有字符串)否則只是 output 這個詞..
剩下的就是把它串起來,你知道怎么做: string.Join(" ", ...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.