如何在c#中的两个字符串之间拆分字符串?
我有一个字符串变量,其中包含HTML数据。现在我想将该HTML字符串拆分为多个字符串,然后最终将这些字符串合并为一个字符串 这是html字符串:如何在c#中的两个字符串之间拆分字符串?,c#,string,c#-4.0,string-parsing,C#,String,C# 4.0,String Parsing,我有一个字符串变量,其中包含HTML数据。现在我想将该HTML字符串拆分为多个字符串,然后最终将这些字符串合并为一个字符串 这是html字符串: <p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</strong></span></p> <p style="text-align: center;"><stron
<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</strong></span></p>
<p style="text-align: center;"><strong><span style="color: #008000;">para2</span> स्द्स्द्सद्स्द para2 again<br /></strong></p>
<p style="text-align: left;"><strong><span style="color: #0000ff;">para3</span><br /></strong></p>
<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</strong></span><strong><span style="color: #008000;">para2</span>para2 again<br /></strong><strong><span style="color: #0000ff;">para3</span><br /></strong></p>
para1
para2स्द्स्द्सद्स्द 再次使用para2
para3
这是我的预期输出:
<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</strong></span></p>
<p style="text-align: center;"><strong><span style="color: #008000;">para2</span> स्द्स्द्सद्स्द para2 again<br /></strong></p>
<p style="text-align: left;"><strong><span style="color: #0000ff;">para3</span><br /></strong></p>
<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</strong></span><strong><span style="color: #008000;">para2</span>para2 again<br /></strong><strong><span style="color: #0000ff;">para3</span><br /></strong></p>
para1para2para2再次
para3
我的拆分逻辑如下所示
标记将HTML字符串拆分为令牌开头的任何标记。下面是regex示例
String pattern = @"(?<=<p.*>).*(?=</p>)";
var matches = Regex.Matches(text, pattern);
StringBuilder result = new StringBuilder();
result.Append("<p>");
foreach (Match match in matches)
{
result.Append(match.Value);
}
result.Append("</p>");
String pattern=@”(?如果要将一个字符串
拆分为另一个字符串
,可以使用String.split(String[]分隔符,StringSplitOptions选项)
其中分隔符
是一个字符串
数组,其中至少包含一个用于拆分字符串的字符串
示例
//Initialize a string of name HTML as our HTML code
string HTML = "<p><span style=\"text-decoration: underline; color: #ff0000;\"><strong>para1</strong></span></p> <p style=\"text-align: center;\"><strong><span style=\"color: #008000;\">para2</span> स्द्स्द्सद्स्द para2 again<br /></strong></p> <p style=\"text-align: left;\"><strong><span style=\"color: #0000ff;\">para3</span><br /></strong></p>";
//Initialize a string array of name strSplit to split HTML with </p>
string[] strSplit = HTML.Split(new string[] { "</p>" }, StringSplitOptions.None);
//Initialize a string of name expectedOutput
string expectedOutput = "";
string stringToAppend = "";
//Initialize i as an int. Continue if i is less than strSplit.Length. Increment i by 1 each time you continue
for (int i = 0; i < strSplit.Length; i++)
{
if (i >= 1) //Continue if the index is greater or equal to 1; from the second item to the last item
{
stringToAppend = strSplit[i].Replace("<p", "<"); //Replace <p by <
}
else //Otherwise
{
stringToAppend = strSplit[i]; //Don't change anything in the string
}
//Append strSplit[i] to expectedOutput
expectedOutput += stringToAppend;
}
//Append </p> at the end of the string
expectedOutput += "</p>";
//Write the output to the Console
Console.WriteLine(expectedOutput);
Console.Read();
//初始化一个名为HTML的字符串作为我们的HTML代码
字符串HTML=“para1para2स्द्स्द्सद्स्द 再次使用para2
para3
”;
//初始化名称strSplit的字符串数组以拆分HTML
string[]strSplit=HTML.Split(新字符串[]{“”},StringSplitOptions.None);
//初始化名为expectedOutput的字符串
字符串expectedOutput=“”;
字符串stringToAppend=“”;
//将i初始化为int。如果i小于strSplit.Length,则继续。每次继续时,将i递增1
对于(int i=0;i=1)//如果索引大于或等于1,则继续;从第二项到最后一项
{
stringToAppend=strSplit[i]。替换(“当你需要解析html时使用。@TimSchmelter:谢谢你为我推荐html Agility pack…今天我学到了非常有用的一个…不应该
是]*>
?这是一样的,因为正则表达式默认使用贪婪搜索。我知道,我只是没有意识到这一点,所以我今天学到了一些新东西。谢谢!)@Saravanan很抱歉,但我不能真正理解你的评论。请你提供你到底想做什么?:)我想替换标记。这意味着我只有一个标记将位于html字符串开始位置,还有一个标记将位于html字符串结束位置。不在任何其他位置…@Saravanan我已更新了我的答案。很抱歉误解。祝您愉快:)