Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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#在忽略空格的情况下替换特定文本_C#_Text_Replace_Space_Ignore - Fatal编程技术网

C#在忽略空格的情况下替换特定文本

C#在忽略空格的情况下替换特定文本,c#,text,replace,space,ignore,C#,Text,Replace,Space,Ignore,我需要替换C#中的文本,同时忽略任何空格 例如: "This is a text with some tags <UL> <P> <LI>", "This is a text with some tags <UL> <P> <LI>", "This is a text with some tags <UL><P> <LI>" or "This is a text wi

我需要替换C#中的文本,同时忽略任何空格

例如:

"This is a text with some tags <UL> <P> <LI>", 
"This is a text with some tags <UL>   <P>    <LI>", 
"This is a text with some tags <UL><P>    <LI>" or 
"This is a text with some tags <UL><P><LI>"
“这是一个带有一些标签的文本

  • ”, “这是一个带有一些标签的文本,

    • ”, “这是带有一些标签的文本

      • ”或 “这是带有一些标签的文本

必须全部替换为

"This is a text with some tags <UL><LI>"
“这是一个带有一些标签的文本
请注意,我不能从整个字符串中删除空格,然后替换所需的字符串,因为这样会产生错误的结果-

"Thisisatextwithsometags<UL><LI>"
“这是带有标签的文本
我确信这3个标签

"<UL>", "<P>" and "<LI>"
    ”、“

    ”和“


将按该顺序出现,但不确定它们之间的空格。

使用
字符串。替换

string text = "This is a text with some tags <UL>   <P>    <LI>";
int indexOfUl = text.IndexOf("<UL>");
if (indexOfUl >= 0)
{
    text = text.Remove(indexOfUl) + text.Substring(indexOfUl).Replace(" ", "").Replace("<P>","");
}
string text=“这是一个带有一些标签的文本

  • ”; int IndexOf=text.IndexOf(“
      ”); 如果(索引>=0) { text=text.Remove(indexOfUl)+text.Substring(indexOfUl).Replace(“,”).Replace(“

      ”,”); }

旧答案(上次编辑前已完成):

string[]text=new[]{“

  • ”、“

    • ”、“

      • ”、“

        • ”; for(int i=0;i”,”); }
或者-由于问题不太清楚(“必须全部替换为
”):

/。。。
文本[i]=“
  • ”;/;-)
使用正则表达式玩得开心

Regex.Replace("<UL>   <P>    <LI>", "<UL>.*<LI>", "<UL><LI>", RegexOptions.None);
Regex.Replace(“

  • ”、“
      *
    • ”、“
      • ”、RegexOptions.None”);
用需要修改的字符串替换第一个参数,如果有
    (任何字符,无论它们包含空格)
  • ,它将用
    • 替换所有参数,尝试使用正则表达式:

      Regex.Replace(inputString, "> *<", "><");
      

      Regex.Replace(inputString,”>*假设每个字符串中都有
        标记

          string[] stringSeparators = new string[] { "<UL>" };
          string yourString = "This is a text with some tags <UL><P><LI>";
          string[] text = yourString.Split(stringSeparators, StringSplitOptions.None);
          string outPut = text [0]+" "+ ("<UL>" + text[1]).Replace(" ", "").Replace("<P>", "");
        
        string[]stringSeparators=新字符串[]{“
          ”}; string yourString=“这是一个带有一些标记的文本

          • ”; string[]text=yourString.Split(stringseparator、StringSplitOptions.None); 字符串输出=文本[0]+“”+(“
              ”+文本[1])。替换(“,”)。替换(“

              ”,”);

        看看这里


        同样对于重放,使用

        -1不够详细,标记不正确。忽略标记用于源代码管理系统,我们称之为“忽略”"-文件机制。我事先不确定文本之间会有多少空格。因此无法将其放入数组。@user2751130:我不理解您的评论。您提供了几个要修改的示例字符串。我已将这些字符串添加到集合中,以向您展示如何删除空格和

        -tags。所以空格与数组没有任何关系。我还可以删除数组并使用三个不同的字符串变量。嘿@TimSchmelter,OP添加了详细信息。这个答案不再适用于他的目的。只是想让你知道。-1这根本不起作用。在他的第一个示例中,这是结果:这是一个带有一些标记的文本

        • .他不想

          留下来.看我的答案。

          Regex.Replace(inputString, "> *<", "><");
          
            string[] stringSeparators = new string[] { "<UL>" };
            string yourString = "This is a text with some tags <UL><P><LI>";
            string[] text = yourString.Split(stringSeparators, StringSplitOptions.None);
            string outPut = text [0]+" "+ ("<UL>" + text[1]).Replace(" ", "").Replace("<P>", "");