将前面和后面的文本添加到文本文件中的每个字符串

发布于 2024-10-17 06:48:33 字数 154 浏览 4 评论 0原文

我有一个文本文件,其中有一堆链接,这些链接全部由空格分隔。我想在每个链接周围添加一个“标签”,使其看起来像这样:

<g:price>linkfromfilegoeshere</g:price>  

i have a text file that has a bunch of links that are are all delimited by whitespace. i want to add a 'tag' around each link so it looks like this:

<g:price>linkfromfilegoeshere</g:price>  

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2024-10-24 06:48:33

不确定我理解是否正确,但这里有一个不整洁的版本,适用于较小的文件。

        string s = File.ReadAllText("FilePath");

        var words = s.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries);

        List<string> tagwords = new List<string>();

        foreach (var word in words)
            tagwords.Add(string.Format("<g:price>{0}</g:price>", word));

        string newtext = string.Join("", tagwords);

        File.WriteAllText("Filepath", newtext);

这可以做得更好,但它是一个开始:)当我稍后有更多时间时我会更新。

Not sure I understand correctly, but here is an untidy version that would work for smaller files.

        string s = File.ReadAllText("FilePath");

        var words = s.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries);

        List<string> tagwords = new List<string>();

        foreach (var word in words)
            tagwords.Add(string.Format("<g:price>{0}</g:price>", word));

        string newtext = string.Join("", tagwords);

        File.WriteAllText("Filepath", newtext);

This could be done a lot better, but its a start :) I will update when I have more time later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文