C# 替换字符串的一部分 [英] C# Replace part of a string
本文介绍了C# 替换字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何替换具有潜在未知起始索引的字符串的一部分.例如,如果我有以下字符串:
How can I replace part of a string that has a potentially unknown starting index. For instance, if I had the following strings:
"<sometexthere width='200'>"
"<sometexthere tile='test' width='345'>"
我希望替换可能具有未知值和未知起始索引之前提到的宽度属性值.
I would be looking to replace the width attibute value that can have an unknown value and as mentioned before an unknown start index.
我知道我必须以某种方式将此基于以下部分,这是不变的,我只是不太明白如何实现这一点.
The I understand that I would somehow have to base this on the following part, which is constant, I just don't quite understand how to achieve this.
width='
推荐答案
using System.Text.RegularExpressions;
Regex reg = new Regex(@"width='\d*'");
string newString = reg.Replace(oldString,"width='325'");
这将返回一个具有新宽度的新字符串,前提是您在新宽度字段中的 ' ' 之间放置了一个数字.
This will return a new string with a new width, provided you put a number between the ' ' in the new width field.
这篇关于C# 替换字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文