c# - 如何分割这个字符串并识别最后一个 '*' 之后的第一个句子?

标签 c# string

我从数据库中获取了类似于以下内容的字符串:

The object of the following is to do:
* blah 1 * blah 2 * blah 3 * blah 4. Some more extremely uninteresting
text. Followed by yet another sentence
full of extremely uninteresting text.
Thankfully this is the last sentence.

I need to format this so that each * represents a bullet point, and the sentence after the last * goes onto a new line, ideally as follows:

The object of the following is to do: 

* blah 1
* blah 2 
* blah 3 
* blah 4. 

Some more extremely uninteresting
text. Followed by yet another sentence
full of extremely uninteresting text.
Thankfully this is the last sentence.

It's easy enough to split the string by the * character and replace that with <br /> *. I'm using the following for that:

string description = GetDescription();

description = description.Replace("*", "<br />*"); // it's going onto a web page.

但这给我的结果是:

The object of the following is to do: 

* blah 1 
* blah 2 
* blah 3 
* blah 4. Some more extremely uninteresting text. Followed by yet
another sentence full of extremely
uninteresting text. Thankfully this is
the last sentence.

我在识别最后一个“*”之后的第一句话时遇到了一些困难,所以我也可以在那里休息一下。有人可以告诉我如何做到这一点吗?

最佳答案

您需要某种方法来确定最后一句中的其他分隔符。您可以对 * 进行全局替换至<br /> *就像您当前所做的那样,然后找到 * 的最后一个实例并替换下一个 .或任何带有 . <br /> 的分隔符。

更新

修正了 <br /> . 的顺序至. <br />正如@Shaded 指出的。

关于c# - 如何分割这个字符串并识别最后一个 '*' 之后的第一个句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3070627/

相关文章:

HttpWebRequest 方法 "This stream does not support seek operations."的 C# 异常 "PUT"

c# - 如何从 C# 主机应用程序执行 Javascript 回调函数

c# - 我可以将 C# 字符串插值与 Linq to SQL 一起使用吗

php - 如何使用 PHP 创建随机字符串?

sql - 如何从 T-SQL 中的字符串中删除扩展 ASCII 字符?

c# - 使用隐式类型

java - 使用 JSOUP 获取 HTML 字符串中的第一个有效图像

c - 存储使用空格输入的字符串

string - Excel VBA - 在单元格之间复制文本并修改文本

c# - 悬停时菜单跳转