stringbuilder数据无法正常显示

编程入门 行业动态 更新时间:2024-10-26 11:16:19
本文介绍了stringbuilder数据无法正常显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

sb.Append( < tr>< td>通话时间: + searchResult [i] .CreatedDate!= null ?Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt): + < / td>< / tr>);

在上面的代码中当我删除下面的代码然后tr显示否则显示时间不标签通话时间:

+ searchResult [i] .CreatedDate!= null?Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt ): +

示例:如果我使用最上面的代码,那么它只显示下午1:00,但实际数据将是通话时间:下午1:00 。当我删除数据值时,它显示调用时间: 在上面的代码中,sb是StringBuilder。 为什么它在stringbuilder中发生我无法找到。 Plz帮助。 谢谢你。

解决方案

注意运算符优先级 [ ^ ] - 条件运算符具有非常低的优先级,仅在+(连接)之后。您可以使用括号更改评估或将其拆分为多个语句。

sb.Append( < tr>< td>通话时间: +(searchResult [i] .CreatedDate!= null ?Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt): )+ < / td>< / tr>);

var callTime = searchResult [i] .CreatedDate!= null ? Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt): ; sb.Append( < tr>< td>通话时间: + callTime + < / td>< / tr>);

顺便说一句,使用属性可以为空的DateTime的值和HasValue会更好:

var createdDate = searchResult [i] .CreatedDate; var callTime = createdDate.HasValue? createdDate.Value.ToString( hh:mm tt): ;

这是由运营商优先级引起的。您的代码相当于:

if (( < tr>< td>通话时间: + searchResult [i] .CreatedDate)!= null ) { sb.Append(Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt)); } else { sb.Append( + < / td>< / TR>中); }

因为附加到非空字符串的任何内容都会产生非空值,所以你总是会在第一个分支。 忽略那一行 - 这是完全错误的。 要么分开代码到多行:

sb.Append( < tr>< td>通话时间:); if (searchResult [i] .CreatedDate!= null ) { sb.Append(Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt )); } sb.Append( < / td>< / tr>);

或添加括号以强制优先:

sb.Append( < tr>< td>通话时间: +(searchResult [i] .CreatedDate!= null ?Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt): )+ < / td>< / t>);

或使用 AppendFormat :

sb.AppendFormat( < tr>< td>通话时间:{0:hh:mm tt}< / td>< / t> ,searchResult [i] .CreatedDate);

Brackets!

// 此 sb.Append( < table>< tbody>< tr>< td>通话时间: + searchResult [i] .CreatedDate!= null ? Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt): + < / TD>< / TR>< / tbody的>< /表>); // 就像这样 sb.Append(( < table>< tbody>< tr>< td>通话时间: + searchResult [i] .CreatedDate!= null ?Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt): )+ < / td>< / tr>< / tbody>< / table>); // 你需要这个 sb.Append( < table>< tbody>< tr>< td>通话时间: +( searchResult [i] .CreatedDate!= null ?Convert.ToDateTime(searchResult [i] .CreatedDate).ToString( hh:mm tt): )+ < / td>< / tr>< / tbody>< / table> ;);

sb.Append("<tr><td>Call Time: " + searchResult[i].CreatedDate != null ? Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt") : "" + "</td></tr>");

in the above code when i remove below code then tr is showing otherwise it showing Time not label "Call Time:"

" + searchResult[i].CreatedDate != null ? Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt") : "" + "

Example:If i use the uppermost code then it's showing 1:00 PM only but actual data will be Call Time: 1:00 PM.When i remove the datavalue then it's showing Call Time: In the above code sb is StringBuilder. Why it's happening in stringbuilder i am unable to find. Plz help. Thank u.

解决方案

Mind the operator precedence[^] - conditional operator has very low priority and only comes after + (concatenation). You can either use parentheses to change the evaluation or split it into multiple statements.

sb.Append("<tr><td>Call Time: " + (searchResult[i].CreatedDate != null ? Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt") : "") + "</td></tr>");

var callTime = searchResult[i].CreatedDate != null ? Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt") : ""; sb.Append("<tr><td>Call Time: " + callTime + "</td></tr>");

BTW it would be much nicer to use properties Value and HasValue of the nullable DateTime:

var createdDate = searchResult[i].CreatedDate; var callTime = createdDate.HasValue ? createdDate.Value.ToString("hh:mm tt") : "";

This is due to operator precedence. Your code is equivalent to:

if (("<tr><td>Call Time: " + searchResult[i].CreatedDate) != null) { sb.Append(Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt")); } else { sb.Append("" + "</td></tr>"); }

Since anything appended to a non-null string will always produce a non-null value, you're always going to end up in the first branch. Ignore that line - it's totally wrong. Either split the code in to multiple lines:

sb.Append("<tr><td>Call Time: "); if (searchResult[i].CreatedDate != null) { sb.Append(Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt")); } sb.Append("</td></tr>");

or add parentheses to force the precedence:

sb.Append("<tr><td>Call Time: " + (searchResult[i].CreatedDate != null ? Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt") : "") + "</td></t>");

or use AppendFormat:

sb.AppendFormat("<tr><td>Call Time: {0:hh:mm tt}</td></t>", searchResult[i].CreatedDate);

Brackets!

//this sb.Append("<table><tbody><tr><td>Call Time: " + searchResult[i].CreatedDate != null ? Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt") : "" + "</td></tr></tbody></table>"); //is like this sb.Append(("<table><tbody><tr><td>Call Time: " + searchResult[i].CreatedDate != null ? Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt") : "") + "</td></tr></tbody></table>"); //you need this sb.Append("<table><tbody><tr><td>Call Time: " + (searchResult[i].CreatedDate != null ? Convert.ToDateTime(searchResult[i].CreatedDate).ToString("hh:mm tt") : "") + "</td></tr></tbody></table>");

更多推荐

stringbuilder数据无法正常显示

本文发布于:2023-11-08 08:03:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正常显示   数据   stringbuilder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!