看到堆好玩的代码之"string.format新用"


  base .Response.Write( string .Format( " \r\n\t\t\t<script language='javascript'>\r\n\t\t\t<!--\r\n\t\t\t\tvar mMultipleSelectionMode = false;\r\n\t\t\t\tfunction __doBodyLoad(selMode)\r\n\t\t\t\t{{\r\n\t\t\t\t\tmMultipleSelectionMode = selMode == 'MULTIPLE';\r\n\t\t\t\t}}\r\n\t\t\t\t__doBodyLoad('{0}');\r\n\t\t\t// -->\r\n\t\t\t</script> " , text2));
      
this .LoadListBoxLeft(text1);
      
this .LoadListBoxRight(text2);
      
this .ListBoxLeft.Attributes.Add( " ondblclick " " __doAddSelectedItem(ListBoxLeft, ListBoxRight); return false; " );
      
this .ListBoxRight.Attributes.Add( " ondblclick " " __doRemoveSelectedItem(ListBoxRight); return false; " );
      
this .btnOK.Attributes.Add( " onclick " " __doReturnDialogResult(this.form); return false; " );

看明白了吗?将简单的串作为一种类似"函数入参"的概念出现。

有时,我们需要在代码里写入SQL串(项目决定,有时就算不喜欢也是情非得已呢),我们需要丢一些查询串,这时,我们就可以用到string.Format的这种功能来实现,不仅增加了代码的可读性,以后扩展起来也更轻松呢,毕竟把一堆的查询串一点一点丢进去着实让人头疼。

当然,这只是一种例子,还有连接串的使用等,都可以使用这种方法。

你可能感兴趣的:(string.Format)