在C#中比较字符串非常简单.事实上,有几种方法可以做到这一点.我在下面的块中列出了一些.我很好奇的是它们之间的差异以及何时应该使用其他的?是否应该不惜一切代价避免?还有更多我没有列出?
string testString = "Test";
string anotherString = "Another";
if (testString.CompareTo(anotherString) == 0) {}
if (testString.Equals(anotherString)) {}
if (testString == anotherString) {}
Run Code Online (Sandbox Code Playgroud)
(注意:我在这个例子中寻找平等,不小于或大于,但也可以随意发表评论)
我遇到了StreamWriter和Byte Order Marks的问题.该文档似乎表明Encoding.UTF8编码已启用字节顺序标记,但是当写入文件时,一些文件具有标记,而其他文件没有.
我正在以下列方式创建流编写器:
this.Writer = new StreamWriter(this.Stream, System.Text.Encoding.UTF8);
Run Code Online (Sandbox Code Playgroud)
任何有关可能发生的事情的想法都将受到赞赏.
使用VS 2008,我有一个Repeater控件:
<asp:Repeater runat="server" ID="storesRep" DataSourceID="storeSqlDataSource"
OnItemDataBound="StoresRep_ItemDataBound">
<ItemTemplate>
<table style="padding:0px">
<tr>
<td style="width:200px"><asp:Label ID="infoLbl" runat="server">
Choose stores for upload:</asp:Label>
</td>
<td style="width:110px">
<asp:Label ID="storeLbl" runat="server" Text='<%# Bind("Name") %>'>
</asp:Label>
</td>
<td><asp:CheckBox runat="server" ID="storeCheck" /></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="storeSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:someConnectionString %>"
SelectCommand="SELECT [StoreId], [Name] FROM [Store] Order By [Name]">
</asp:SqlDataSource>
Run Code Online (Sandbox Code Playgroud)
现在,如果数据源没有从数据库返回任何项目,我想显示默认文本,如"找不到商店".到目前为止,我主要使用的GridView
是因为EmptyDataText
属性而没有问题的地方.
我使用MVC4 web-api,c#,并希望使用Json.net返回Json .
问题是它带有"反斜杠".
我还将此代码添加到Global.asax.
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
Run Code Online (Sandbox Code Playgroud)
这是它的回报.
"{\"cid\":1,\"model\":\"WT50JB\",\"detail\":\"sdf??\",\"unit\":2,\"time_in\":\"2012-12-11T19:00:00\",\"time_out\":\"2012-12-12T13:00:06.2774691+07:00\",\"time_used_dd\":0.0,\"time_used_hh\":0.0}"
Run Code Online (Sandbox Code Playgroud)
所以我想看到的是
{"cid":1,"model":"WT50JB","detail":"sdf??","unit":2,"time_in":"2012-12-11T19:00:00","time_out":"2012-12-12T13:08:50.5444555+07:00","time_used_dd":0.0,"time_used_hh":0.0}
Run Code Online (Sandbox Code Playgroud)
这是JsonConvertor
string json = JsonConvert.SerializeObject(myObj);
Run Code Online (Sandbox Code Playgroud) appsettings
我的.Net核心应用程序中有3个特定于环境的文件
在project.json
我这样设置publishOptions
.(根据此处的建议)
"publishOptions": {
"include": [
"wwwroot",
"appsettings.development.json",
"appsettings.staging.json",
"appsettings.production.json",
"web.config"
]
},
Run Code Online (Sandbox Code Playgroud)
我有3个相应的启动类,它们使用适当appsettings
的环境
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true);
Run Code Online (Sandbox Code Playgroud)
但是,当我发布应用程序时,所有3个appsettings文件都会在所有环境中结束.如何发布特定于环境的appsetting文件?
我在.net页面上有一些文本框,并希望用jQuery实现以下功能:如果用户按下返回,程序应该"好像"他使用了tab键,因此,选择下一个元素.我尝试了以下代码(还有一些代码):
<script type="text/javascript">
jQuery(document).ready(function () {
$('.tb').keypress(function (e) {
if (e.keyCode == 13)
{
$(this).trigger("keydown", [9]);
alert("return pressed");
}
});
});
Run Code Online (Sandbox Code Playgroud)
<asp:TextBox ID="TextBox1" runat="server" CssClass="tb"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" CssClass="tb"></asp:TextBox>
Run Code Online (Sandbox Code Playgroud)
但它只是不起作用!错过了什么,弄错了?
这里有一些我用过的链接
我需要在asp.net发送电子邮件,但我需要发件人看起来像"MySiteName"而没有info@mysitename.com.
我使用ASP.NET并在我的页面上有一个标签控件,我用jQuery-Command填充它
$('#<%= myLabel.ClientID %>').html(content);
Run Code Online (Sandbox Code Playgroud)
.val()似乎不适用于此.
不知何故,我有问题在代码隐藏中获取内容.在代码中,myLabel.Text-Property仍为空.
我学习使用.Net的内置配置文件提供程序,并遇到以下问题:
我读到可以通过.Net-Application的web.config-settings覆盖machine.config-settings.machine.config文件中的以下设置与我相关:
<connectionStrings>
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;
Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<profile><providers><add name="AspNetSqlProfileProvider"connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/></providers></profile>
Run Code Online (Sandbox Code Playgroud)
这些设置用于设置本地配置文件.但是,当我将设置复制到我的应用程序的web.config并更改machine.config设置,以便它们不再工作时,我收到配置错误.例如,我将machine.config中提供程序的名称更改为"Local".这应该没问题,因为覆盖了设置.但是,在运行应用程序时,我收到错误:
"条目"AspNetSQLProvider已经添加"(我的翻译)
我需要一个正则表达式来匹配0到99之间的任何数字.可能不包括前导零,这意味着f.ex. 05是不允许的.
我知道如何匹配1-99,但不包含0.
我对1-99的正则表达式是
^[1-9][0-9]?$
Run Code Online (Sandbox Code Playgroud)