【问题标题】:How do you split a string if you know the start and stop point but not the middle in asp.net C#如果您知道起点和终点但不知道asp.net C#中的中间点,如何拆分字符串
【发布时间】:2018-12-20 20:08:28
【问题描述】:

我正在尝试删除可以随时更改的字符串部分。这使得很难拆分字符串以删除所述部分。在要删除的部分之前和之后有一个点是不变的。

这是我能想到的准确展示示例的唯一方法(请忽略它是 html 的事实):

string text = 
"<ul>
<li>keep this text</li>
<li class=Known  unknown text  </li>
<li>keep this text</li>
</ul>";  

string [] splitPerams = {"","<li class=Known (im guessing a regex here) 
</li>"}

string [] results = 
text.Split(splitPerams,System.StringSplitOptions.RemoveEmptyEntries);

输出:

"<ul>
<li>keep this text</li>

<li>keep this text</li>
</ul>";

我知道关于这个主题有很多类似的问题,但它们都是用不同的语言编写的,我不知道如何在 c# 中实现逻辑。

编辑:我想我不允许删除它,所以我会尽我所能完全改写它以便更好地理解。

【问题讨论】:

  • 你不应该使用字符串操作来处理 HTML,有专门的库来处理这个问题:HtmlAgilityPackAngleSharp 是最受欢迎的两个
  • @maccettura 我正在进行的项目需要这个。但是,这个问题是指如何拆分字符串。字符串的内容应该与此无关。如果它更有帮助,我将给出一个不同的字符串示例。
  • 好吧,如果您的现实生活中的输入 是 HTML,那么请使用我所说的库。如果您想在不使用库的情况下执行此操作,那么您可以随意查看库的功能,因为它们是开源的。
  • 您的新示例更没有意义。如果您只想要第一个 x 位数,只需使用 Substring()
  • 这与其说是拆分,不如说是截断。你不能只使用子字符串吗? docs.microsoft.com/en-us/dotnet/api/…

标签: c# html asp.net split


【解决方案1】:

听起来您需要的不是拆分,而是字符串的前端,因此使用substring 应该可以做到。由于您知道删除部分以哪些字符开头,因此使用 indexOf 会很有用。

var str = "Hi My name is Mr. ???? from the usa.";
var newStr = str.Substring(0, str.IndexOf("Mr."));

玩弄它以获得您想要的确切长度。

参考资料:

https://docs.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.7.2

https://docs.microsoft.com/en-us/dotnet/api/system.string.indexof?view=netframework-4.7.2

【讨论】:

  • 他不是要删除 newStr 的结果吗?在这种情况下,您可以在 str 上使用 Regex.Replace,因为您知道未知文本(在 newStr 中)。
  • 在多次编辑的迭代之一中,他给出了我所拥有的str,而他想要的结果是"Hi My name is"。在我的 sn-p 中,这就是 newStr 中的内容(根据他的确切要求加或减一个空格字符)。
  • 如果我只需要 Mr. 之前的文字,这对我来说是不好的措辞,因此所有的编辑都会起作用。对不起。
【解决方案2】:

如果这对某人有帮助,当您知道未知部分之前和之后的部分时,这是从字符串中删除/提取未知文本部分的方法。

string originalText = "Hi my name is Mr. Smith from the USA.";

string[] topPull = { "", "Mr." };
string[] bottomPull = { "from", "" };
string result;

string[] topPage = originalText.Split(topPull,StringSplitOptions.RemoveEmptyEntries);
string[] bottomPage = 
originalText.Split(bottomPull,StringSplitOptions.RemoveEmptyEntries);


//topPage[0] gives all text above topPull, but not topPull it's self
//bottomPull[1] gives all text below bottomPull, but not bottomPull it's self
//now that we have grabbed all the text above and below our known sections we need to 
//add in the known sections themselves, ie topPull and bottomPull

result = topPage[0] + topPull[1] + " " + bottomPull[0] + bottomPage[1];

输出:“嗨,我的名字是来自美国的先生。”


如果你只想保留中间的文字,你可以这样做

string originalText = "Hi my name is Mr. Smith from the USA.";

string[] topPull = { "", "Mr." };
string[] bottomPull = { "from", "" };
string result;

string[] topPage = originalText.Split(topPull,StringSplitOptions.RemoveEmptyEntries);
string[] bottomPage = 
topPage[1].Split(bottomPull,StringSplitOptions.RemoveEmptyEntries);
result = bottomPage[0];

输出:“史密斯”;

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多