可能重复:
使用条件?:(三元)运算符的好处条件运算符
是否缓慢?
大家好,
关于if/else语句的不同,我有一个非常简单的问题.
除了编写更少的代码之外,使用条件运算符而不是完整的if/else语句还有其他好处吗?
是否有性能提升,编译代码减少,或者在使用时有什么好处?
感谢您的帮助
马尔科
我正在尝试在我的jQuery脚本中添加一个函数,该函数根据其alt属性为img构建一个完整的src路径.我们的想法是尽可能使代码变得纤薄,以便处理它的非技术人员不会破坏任何东西; 他们所要做的就是让alt属性正确,路径的其余部分由脚本自动构建.
无论如何,我的文件名包含连字符,为了使其更加万无一失,我想在alt属性中允许空格,这些空格将被替换为src属性中的连字符.麻烦的是,.replace()命令似乎只对第一个匹配的字符起作用,所以如果我在alt属性中有三个单词来描述img,则第二个空格不会被替换而img路径会中断.
这是有问题的代码:
<div class="copy"><img alt="three word alt" /></div>
<script>
$('div.copy').find('img').each(function() {
$(this).attr('src','/images/'+$(this).attr('alt').replace(' ','-')+'.png');
});
</script>
Run Code Online (Sandbox Code Playgroud)
最终结果应该是
<img src="/images/three-word-alt.png" alt="three word alt" />
Run Code Online (Sandbox Code Playgroud)
但相反它出来是这样的:
<img src="/images/three-word alt.png" alt="three word alt" />
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法呢?
我发誓我已经在整个谷歌数据库中搜索了一个可能的解决方案,但我仍然坚持这个问题:)
基本上,我在数据库表中有12行,我在其中生成一个带有4个UITextField的自定义UITableViewCell.表列看起来像这样(SQLITE)
EntryFieldID NUMBER
Description TEXT
FieldType TEXT
Run Code Online (Sandbox Code Playgroud)
现在我希望每个UITableViewCellTag都是EntryFieldID上面的,所以我可以很容易地再引用它来存储在另一个表中.那张桌子看起来像这样.
OrderID NUMBER
EntryFieldID NUMBER
Value1 TEXT
Value2 TEXT
Value3 TEXT
Value4 TEXT
Run Code Online (Sandbox Code Playgroud)
4个Value*字段是UITextFields每个中的4个字段UIViewCell.
希望到目前为止这是有道理的:)
现在在我的TableViewSource里面我有这个代码:
protected List<InfoCaptureTableViewGroup> _tableItems;
protected string _customCellIdentifier = "InfoCaptureField";
protected Dictionary<int, InfoCaptureTableViewCell> _cellControllers = new Dictionary<int, InfoCaptureTableViewCell>();
public InfoCaptureTableSource (List<InfoCaptureTableViewGroup> items)
{
this._tableItems = items;
}
/// <summary>
/// Called by the TableView to determine how many sections(groups) there are.
/// </summary>
public override int NumberOfSections …Run Code Online (Sandbox Code Playgroud) 情景是这样的.
我已经构建了一个顶级导航原型,需要在它上面有一个叠加(透明PNG)图像.它目前覆盖了大约1/3的链接.请看下面:

有什么方法可以让前1/3的链接响应甚至 - 尽管有<div />部分覆盖它们吗?叠加层不包含任何可点击的内容,它只是一个设计功能.
我从来没有这样做过,也不会想象这是可能的,但我期待被证明是错的.
Javascript/jQuery 不是首选,但将作为最后的手段.
这是jsFiddle的测试用例,它直接模仿我当前代码的结构.
如果我访问1网站www.abc.com,其中提到了在Google Code上托管的jQuery,然后我访问了第二个网站www.xyz.com,该网站具有相同的参考 - 该文件是否会在两个网站上缓存?即它会在第二个网站上重新下载,还是只从缓存中使用它?
我目前正在构建一个半复杂的计算器,它基本上是我提供的Excel电子表格的转换.
我已经把大部分内容钉在了但是在Excel电子表格中有一个部分,其中在6行和7列之间进行了多次计算,但问题在于计算没有按照特定的顺序发生.
因此,例如,Row0[Column1]使用计算,并使用(Row2[Column4] * Row2[Column5])等Row1[Column4]计算(Row4[Column2] / Row5[Column1])..你得到了想法.
我曾考虑使用2D数组,但我担心这些值会按特定顺序计算,因此在达到它们时没有任何价值.据我所知,ROW1将首先计算,然后行2,ROW3,等等.
因此,如果没有为我的Excel电子表格中的每个单元格创建变量(并对其进行适当排序),有没有办法可以使用C#计算出来?
我真的很感激任何帮助,建议,指针,无论你认为什么都可能 - 我很乐意听到它!
编辑实现@dtb提供的Lazy类之后,我得到了以下代码.它是我提供的Excel电子表格中的内容的直接副本,包括指针和计算.
var sr = new Lazy<decimal>[6, 6];
sr[0, 0] = new Lazy<decimal>(() => sr[1, 0].Value - eNumber);
sr[0, 3] = new Lazy<decimal>(() => sr[0, 4].Value - sr[1, 0].Value - sr[1, 4].Value);
sr[0, 4] = new Lazy<decimal>(() => sr[0, 0].Value * edD);
sr[0, 5] = new Lazy<decimal>(() => sr[0, 0].Value); …Run Code Online (Sandbox Code Playgroud) c# excel calculated-columns lazy-evaluation multidimensional-array
在将字符串(通常)转换为适当的数据类型时,似乎存在很多混淆,同时也在动态验证它.无论我在哪里 - 博客,文章,代码示例,论坛......一些人似乎有一种处理这些场景的首选方式.
案例通常是来自未知来源的字符串/对象,例如QueryString,Session/Viewstate,WebService等等......
我见过很多不同的例子:
假设我们正在尝试获取id=查询字符串并将其作为整数用于我们的代码中.但是有人篡改了我们的网址并将其更改为
单程
int id = Convert.ToInt32(Request["id"]);
// "Input string was not in a correct format"
Run Code Online (Sandbox Code Playgroud)
其他方式
int id = (int)Request["id"];
// "Input string was not in a correct format"
Run Code Online (Sandbox Code Playgroud)
还有另一种方式
int id = int.Parse(Request["id"]);
// "Input string was not in a correct format"
Run Code Online (Sandbox Code Playgroud)
我见过这个 (并且抛出异常并通知用户是有意义的)
int id = 0;
try {
id = Convert.ToInt32(Request["id"]);
}
catch(Exception ex) {
lblError.Text = ex.Message;
}
if(id > 0) { ... }
Run Code Online (Sandbox Code Playgroud)
最后,和我个人使用的那个
int …Run Code Online (Sandbox Code Playgroud) $variable = 'for linebreak add 2 spaces at end';
Run Code Online (Sandbox Code Playgroud)
每次变化时此变量的值.
如何在此字符串之前和之后添加一些文本或html?
例如,如果我们想要在'<div>'之前和'</div>'之后添加,字符串应该如下所示:
$variable = '<div>for linebreak add 2 spaces at end</div>';
Run Code Online (Sandbox Code Playgroud) 我有以下XSLT
<xsl:param name="productsId" select="/macro/productsId" />
<xsl:param name="type" select="/macro/type" /> <!-- value1, value2, value3 -->
<xsl:template match="/">
<xsl:if test="$productsId > 0">
<xsl:variable name="products" select="umbraco.library:GetXmlNodeById($productsId)" />
<div id="carousel-wrap">
<ul id="carousel">
<xsl:for-each select="$products/Product [select only Product with attribute value1, value2 or value3 based on /macro/type]">
<li id="p-{@id}">
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="string-length(productHeading) > 0">
<xsl:value-of select="productHeading" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@nodeName" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<a href="{umbraco.library:NiceUrl(@id)}">
<!-- Image -->
<xsl:if test="productImage > 0">
<xsl:variable name="productImage" select="umbraco.library:GetMedia(productImage, 0)" />
<img src="/ImageGen.ashx?image={$productImage/umbracoFile}&height=131" />
</xsl:if> …Run Code Online (Sandbox Code Playgroud) c# ×3
jquery ×3
replace ×2
caching ×1
css ×1
excel ×1
html ×1
if-statement ×1
ios ×1
iphone ×1
javascript ×1
objective-c ×1
php ×1
string ×1
types ×1
umbraco ×1
xamarin.ios ×1
xml ×1
xpath ×1
xslt ×1