Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何拆分此字符串并识别last'*';?_C#_String - Fatal编程技术网

C# 如何拆分此字符串并识别last'*';?

C# 如何拆分此字符串并识别last'*';?,c#,string,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. 用*字

我从数据库中得到一个类似于以下内容的字符串:

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. 用
*
字符分割字符串并替换为

*
非常简单。为此,我使用以下方法:

string description = GetDescription();

description = description.Replace("*", "<br />*"); // it's going onto a web page.
string description=GetDescription();
description=description.替换(“*”,“
*”);//它会出现在网页上。
但这给我的结果是:

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. 以下工作的目的是: *废话1 *废话2 *废话3 *废话4。一些非常枯燥的文本。接着是 另一句话充满了极端 无趣的文本。谢天谢地,这是 最后一句。
我在识别最后一个“*”后面的第一个句子时有点困难,所以我也可以在那里稍作休息。有人能告诉我怎么做吗?

你需要一些方法来确定你最后一句话中的另一个分隔符。您可以像当前一样将
*
全局替换为

*
,然后找到
*
的最后一个实例,并将下一个
或任何分隔符替换为

更新
修正了

的顺序
正如@shade所指出的。

如果可以假设最后一个“*”后面的第一句用句点分隔,那么您需要在blah 4之后添加另一个*,然后您可以执行以下操作:

  • 通过查找所有
    *
    字符替换输入
  • 找到最后一个

    ,然后找到它后面的第一个
  • 重新组合片段,将
    *
    替换为

    *
  • 这里有一些代码可以帮助你:(它可能并不完美)。我在这里使用的是
    string.Split()
    ,而不是
    string.Replace()
    ,因为我发现它使逻辑更容易理解。诀窍在于最后一个子区域由两部分组成(子弹和子弹后面的句子)

    字符串inputText=“…”;
    字符串[]子区域=inputText.Split(“*”);
    //根据第一个周期将最后一个区域分为项目符号和句子。。。
    字符串[]lastRegions=子区域[subsection.Length-1].Split(新[]{.'},2);
    //用组合零件(项目符号/句子)替换最后一个子区域
    子区域[子区域.Length-1]=字符串.Join(“.
    ”,lastRegions); //将所有子区域合并在一起,用项目符号分隔符替换“*”。 string finalText=string.Join(“
    *”,子区域);
    如果您可以假设最后一个项目符号以“.”结尾,那么类似的方法可能会起作用:

    string description =
        "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 extrememly uninteresting text. Thankfully this is the last sentence.";            
    int idx = description.IndexOf('.', description.LastIndexOf('*'));
    description = description.Insert(idx+1, "<br />");
    description = description.Replace("*", "<br />*");
    
    字符串描述=
    “以下内容的目的是:*废话1*废话2*废话3*废话4.一些非常无趣的文本。接着是另一个充满非常无趣文本的句子。谢天谢地,这是最后一句。”;
    int idx=description.IndexOf('.',description.LastIndexOf('*');
    description=description.Insert(idx+1,“
    ”); description=description.替换(“*”,“
    *”;
    基本上,它会在最后一个“*”之后找到第一个出现的“.”。如果你有更多的分隔符,你也可以使用它们。

    不是一个C#专家,但我会考虑将“*”上的字符串“拆分”成一个数组,以获得所有的项目符号,然后拆分数组中的最后一项(你的最后一个项目符号和句子),或者使用正则表达式来获取句子

    i、 e.伪代码

    List bullets = description.split('*');
    String lastSentence = bullets(bullets.size() + 1).replace('^\*.*?\.', '');
    
    正则表达式的作用是将所有内容剥离到第一个句点


    当然,如果你的laast语句中有一个“*”的话,这种方法可能不起作用。当然:)

    你可能真的想用“*”来代替它
    这样句点就不会随着最后一句话跳转。仍然+1,因为我会这么做。如果我在废话4后面加上另一个*,它将被“
    *”替换,因此在项目符号后面的句子前面会有一个*引导
    string description =
        "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 extrememly uninteresting text. Thankfully this is the last sentence.";            
    int idx = description.IndexOf('.', description.LastIndexOf('*'));
    description = description.Insert(idx+1, "<br />");
    description = description.Replace("*", "<br />*");
    
    List bullets = description.split('*');
    String lastSentence = bullets(bullets.size() + 1).replace('^\*.*?\.', '');