How to replace dynamic string values in asp.net?

半世苍凉 提交于 2021-02-11 15:20:56

问题


In my application requirement are to replace text with line break. Mine code is

pageText = pageText.Replace("<td style=\"width:23.0769230769231%;\">", "<br>");

Here width value is dynamic which is different for different pdf pages. How to replace this whole string with line break by using string.Replace or using Regex ?


回答1:


You want a regex which will replace the string: "<td style=\"width:X;\">" with "<br>" where X is any number?

Console.WriteLine(Regex.Replace(input, "<td style=\"width:\\d+\\.?\\d*%;\">", "<br>"));

.NET fiddle here: https://dotnetfiddle.net/30QCRP




回答2:


Try this:

  string pattern = "<(.*?)>";
  string replacement = "<br>";
  Regex rgx = new Regex(pattern);
  string result = rgx.Replace(input, replacement);

Or yo could be more specific if you start with this:

string pattern = "<td style=(.*?)>";


来源:https://stackoverflow.com/questions/34859351/how-to-replace-dynamic-string-values-in-asp-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!