3

All Customers Take $10 off $50.58 in ccc.com's Home Decor, Home Appliances and Outdoor Store! Valid through 5/31/2012"

I am getting above type of string from db,the string may or may not contain $.If it contains $ then the numbers after the $ should be underlined.The string is completely dynamic.It also may not contain $

4

2 回答 2

7

尝试正则表达式:

Regex.Replace(str, @"(?<=\$)\d+(\.\d+)?", "<u>$0</u>");

一些示例输出:

"4.5"     -> "4.5"         (untouched)
"4"       -> "4"           (untouched)
"$4.5"    -> "$<u>4.5</u>"
"$4"      -> "$<u>4</u>"

虽然,因为u已弃用,但您可能应该考虑<span>使用类或内联样式与text-decoration: underline指令:

Regex.Replace(str, @"(?<=\$)\d+(\.\d+)?", 
  "<span style=""text-decoration: underline"">$0</span>");

这取决于您要将此文本放入的封闭元素是什么:如果它也是 a span,那么您不能嵌套它们,因此您必须关闭第一个(只需"</span>"坚持替换字符串的开头(第三个参数)和"<span>"结尾)

于 2012-05-10T12:42:35.927 回答
0

以上面使用跨度的例子为例......可能比正则表达式更容易阅读的东西只是 indexOf 方法

        string s = "$10";
        string t =  (s.IndexOf('$') > 0) ? "<span style=\"text-decoration: underline\">#</span>".Replace("#",s):"<span>{0}</span>".Replace("#",s);
于 2012-05-10T17:36:17.367 回答