stringbuilder数据无法正常显示 [英] stringbuilder data not showing properly
问题描述
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 < span class =code-string>): +
示例:如果我使用最上面的代码,那么它只显示下午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!= < span class =code-keyword> 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( < span class =code-string> hh:mm tt): )+ < / td>< / tr>< / tbody>< / table>跨度>);
// 你需要这个
sb.Append(< span class =code-string> < 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 useAppendFormat
:
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数据无法正常显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!