字符串修改和替换c#

字符串修改和替换c#,c#,C#,我有一根像 <link rel='stylesheet' href='/abc/def/ghi/default.css' type='text/css' media='screen' /> 我想把它追加回我的原始字符串 <link rel='stylesheet' href='/abc/def/ghi/default.css/v=123' type='text/css' media='screen' /> 我尝试过使用子字符串,但内容总是会发生变化,我可以从“hr

我有一根像

<link rel='stylesheet' href='/abc/def/ghi/default.css' type='text/css' media='screen' />
我想把它追加回我的原始字符串

<link rel='stylesheet' href='/abc/def/ghi/default.css/v=123' type='text/css' media='screen' />


我尝试过使用子字符串,但内容总是会发生变化,我可以从“href”开始查找字符串,但我不知道href需要多长时间。

如果您在字符串中的“href”标识符后标识开始和结束单引号,您仍然可以使用子字符串

public string foo(string inputString)
        {
            string result = "";
            int hrefIndex = inputString.IndexOf("href=");
            int firstQuoteIndexAfterHref = -1;
            int secondQuoteIndexAfterHref = -1;

            if (hrefIndex != -1)
            {
                firstQuoteIndexAfterHref = inputString.IndexOf("'", hrefIndex + 1);
                Console.Out.WriteLine("hrefIndex={0}, firstQuoteIndex={1}", hrefIndex, firstQuoteIndexAfterHref);
                if (firstQuoteIndexAfterHref != -1)
                {
                    secondQuoteIndexAfterHref = inputString.IndexOf("'", firstQuoteIndexAfterHref + 1);
                    result = string.Format("{0}/v=123",inputString.Substring(firstQuoteIndexAfterHref, secondQuoteIndexAfterHref - firstQuoteIndexAfterHref + 1));
                }
            }
            return result;
        }

你可以这样做:

public string foo(string str)
{
    var StartIndex = str.IndexOf("href");
    var firstapostrophe = str.IndexOf("'",StartIndex);
    var secondapostrophe = str.IndexOf("'", firstapostrophe);
    var result = str.Replace(str.Substring(firstapostrophe, secondapostrophe - 2),
    str.Substring(firstapostrophe, secondapostrophe - 2) + "/v=123");
    return result; 
}

简单地将其视为xml,请看以下内容:

using System;
using System.Xml;
using System.Xml.Linq;

public class Program
{
    public static void Main()
    {
        var s = "<link href='aaa' />";
        var doc = XDocument.Parse(s);
        var link = doc.Element("link");
        var href = link.Attribute("href");

        href.Value = "bbb";
        s = doc.ToString();
        Console.WriteLine(s);
    }
}
使用系统;
使用System.Xml;
使用System.Xml.Linq;
公共课程
{
公共静态void Main()
{
var s=“”;
var doc=XDocument.Parse;
var link=单据要素(“link”);
var href=link.Attribute(“href”);
href.Value=“bbb”;
s=doc.ToString();
控制台。写入线(s);
}
}

Use I want in c#codeHtmlAgilityPack是一个可以添加到代码中的nuget包。它为您处理解析HTML。所以你不必自己动手。那是c#还是JS?c#中的方法不能以“function”开头,我再次希望将其附加到
中。因此,将
更改为
以便您可以使用
Replace()
方法,请在答案中查看我的更改。我再次希望将其附加到。因此,更改为首先放置id属性,例如,然后在某些按钮操作或页面加载事件处理程序的代码中放置link1.Href=link1.Href+“/v=123”;
using System;
using System.Xml;
using System.Xml.Linq;

public class Program
{
    public static void Main()
    {
        var s = "<link href='aaa' />";
        var doc = XDocument.Parse(s);
        var link = doc.Element("link");
        var href = link.Attribute("href");

        href.Value = "bbb";
        s = doc.ToString();
        Console.WriteLine(s);
    }
}