【在C#中通过字符串获取成员?】教程文章相关的互联网学习教程文章

c# – 如果字符串以特定单词结尾,则修剪字符串【代码】

如果有一个单词出现在字符串的末尾,我需要修剪字符串. C#中的快速表现方法是什么? 注意:要修剪的单词可以是任何东西.. WHERE只是一个例子string text1 = "My hosue where sun shines"; //RESULT: "My hosue where sun shines" string text2 = "My where"; //RESULT: "My" string text3 = "My where I WHERE"; //RESULT:"My where I"解决方法:你可以使用string.EndsWith方法和string.Substringpublic static string Trim(this st...

如何在C#中使用字符串正常工作?【代码】

我知道C#中有一个关于字符串的规则说:When we create a textual string of type string, we can never change its value! When putting different value for a string variable thje first string will stay in memory and variable (which is kind of reference type) just gets the address of the new string.所以做这样的事情:string a = "aaa"; a = a.Trim(); // Creates a new string不推荐.但是,如果我需要根据用户偏好对...

c# – 为什么ConfigurationSection需要用字符串查找?【代码】

我在网上找到的ConfigurationSection示例(for example)都有如下代码:public class ConnectionSection : ConfigurationSection {[ConfigurationProperty("Servers")]public ServerAppearanceCollection ServerElement{get { return ((ServerAppearanceCollection)(base["Servers"])); }set { base["Servers"] = value; }} }为什么使用方括号从基数访问值“Servers”?是从xml创建此对象时使用的setter,还是用于覆盖xml文件中的值的...

根据c#中的字符串值为某个整数赋值【代码】

假设我有3个整数都声明为int Y = 0; int N = 0; int U = 0;从那里我从存储过程中获取2个值(一个字符串和一个整数).该字符串返回Y,N,U整数返回参加人数.string test = dr["ATTEND_CDE"].ToString(); int test2 = int.Parse(dr["COUNT"].ToString());有没有更好的方法来分配相应的整数除以下计数:if (test == "Y") {Y = test2; } else if (test == "N") {N = test2; } else if (test == "U") {U = test2; }解决方法:你真正拥有的是字...

c# – 如何创建Alphabet和Number的字符串组合系列?【代码】

我有一个数据列表集合,例如:List<String> Dummy = new List<String>() {"1001A","1003A","1002B","1002A","1003B","1001B","1003C","1002C","1001C", };我想把这个数据列表整理成一系列.主要系列将重点关注Alphabet(字符串的最后一个字符),子系列将基于左边的数字.输出将是这样的:1001A 1002A 1003A 1001B 1002B 1003B 1001C 1002C 1003C除了上面的示例之外,我已经只有一系列数字的功能代码.感谢阅读我的帖子.解决方法:如果字符串...

c# – 使用字符串的Console.writeline【代码】

一个简单的问题:如何使用VS.中的C#使用Console.Writeline()在CMD中显示字符串?我知道你用于整数和浮点数.但是你对字符串使用了什么?这就是我所拥有的:private string productName;public void GetItemData(){ShowReciept();}private void ReadItem(){ Console.WriteLine("Enter the product's name: "); productName = Console.ReadLine();}private void ShowReciept(){Console.WriteLine("**** Name of product:", product...

c# – 尝试使用时查询字符串更改.【代码】

我有一个看起来像这样的查询字符串.Page.aspx?S = C94CA8CCAFB12E2B669735186D327D1B3E505538139A66C8455X#53411389BBB9577E1FD然后我有一个回帖看起来像这样的按钮:protected void btn_Click(object sender, EventArgs e) {string URL = Request.QueryString["s"];Response.Redirect("Page2.aspx?s="+URL); }但是当我到达第2页时,我在URL中放入的查询字符串将%符号更改为#并且解密失败,因为它在字符串中查找%来分隔我的所有页...

如何在c#中格式化此字符串【代码】

我有一个存储在数据库中的设置,其值为.jpg | .gif | .png.我希望在前端显示为“.jpg”,“.gif”和“.png”在一个字符串中.例如Allowed formats are “.jpg”,”.gif” and “.png”我能够分开使用它string fileTypes = String.Join(",", supportedFileTypes.Split('|'))如何在最后一个字符串之前指定和子句.它必须是动态的.例如,如果我们在数据库中有.jpg | .gif,它应该是Allowed formats are “.jpg” and “.gif”.解决方法: pub...

c# – Asp.Net查询字符串【代码】

我使用Querystring将值从一个页面传递到另一个页面.我想使用Server.UrlDecode和urlEncode实现编码和解码. 查询字符串返回空值,但我可以检查值是否已在URL中发送. 这两页是: QueryString.aspxprotected void Page_Load(object sender, EventArgs e) {} protected void Button1_Click(object sender, EventArgs e) {string id = "1";string name = "aaaa";string url = string.Format("QueryStringValuesTransfer.aspx?{0}&{1}", Se...

c# – .NET REGEX匹配匹配空字符串【代码】

我有这个 图案:[0-9]*\.?[0-9]*目标:X=113.3413475 Y=18.2054775我想匹配数字.它匹配http://regexpal.com/和Regex Coach等测试软件中的find. 但在Dot net和http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx 我明白了:Found 11 matches:1. 2. 3. 4. 5. 6. 113.3413475 7. 8. 9. 10. 18.2054775 11.String literals for use in programs:C#@"[0-9]*[\.]?[0-9]*"任何人都知道为什么我得...

c# – 将复杂的字符串解析为DateTime【代码】

有人能告诉我如何将以下格式转换为正确的DateTime对象?11:50:46 AM on Wednesday, October 19, 2011解决方法:string s = "11:50:46 AM on Wednesday, October 19, 2011";DateTime dateTime = DateTime.ParseExact(s, "hh:mm:ss tt on dddd, MMMM dd, yyyy", CultureInfo.InvariantCulture);

c#泛型. ,以编程方式从读取字符串值中分配T?【代码】

我有两个类(即Customer和Employee)和一个通用存储库GenericRepository< T>哪里T:上课. 是否可以在从字符串中分配T的值时实例化新的GenericRepository实例? 像这样:string x = "Customer"; var repository = new GenericRepository<x>();(从而创建GenericRepository类型的存储库实例< Customer>)解决方法:是的,但这很尴尬.string name = "MyNamespace.Customer";Type targetType = Type.GetType(name);Type genericType = typeo...

字符串到C#中的dateTime?【代码】

如何将以下日期字符串转换为dateTime:Fri, 18 Dec 2009 9:38 am PST我试过DateTime.Parse(string) 我收到以下错误:The string was not recognized as a valid DateTime. There is an unknown word starting at index 25. System.SystemException {System.FormatException}UPDATE 我试图从雅虎获得天气,我试图得到这样的日期:Date = DateTime.Parse(feed.Element(yWeatherNS + "condition").Attribute("date").Value),我调试了它...

c# – 浮点到字符串转换【代码】

我想将浮点值转换为字符串. 以下是我用于转换的代码.static void Main(string[] args){string s =string.Format("{0:G}", value); Console.Write(s);Console.ReadLine();}它输出为2.5 但我的问题是我希望得到2.50的值,因为我想在我的项目中稍后将其与原始值进行比较. 如果有办法,请建议我吗?解决方法:您应该使用{0:N2}格式化为两位小数.string.Format("{0:N2}", 2.50)小数点后3位:string.Format("{0:N3}", 2.50)...

c# – 如何防止Json.NET将枚举转换为字符串?【代码】

以下课程public class RequestSections : RequestBase {public RequestSections(Command c, Dictionary<SectionIdentifier, BigInteger> v) : base(c){VERSIONS = v;}public Dictionary<SectionIdentifier, BigInteger> VERSIONS { get; set; } }使用JSON.NET序列化为JSON并生成以下JSON输出:{"VERSIONS": {"Photos": 901,"Music": 902},"CMD": 43 }问题是SectionIdentifier是枚举,但JSON.NET将它们转换为字符串.public enum Sect...