Tag: string

将字符串转换为字符串的generics类型

我正在编写一个方法来进行智能类型转换 – 如果类型参数恰好是一个字符串,则使用ToString(),否则转换,但如果转换不起作用则返回null。 基本上可以获得尽可能多的信息,而不会抛出exception。 我在尝试演员之前检查T确实是一个string ,但编译器仍然不是粉丝: Cannot convert type ‘string’ to ‘T’ 这是我的方法: public T? Convert(object v) { if (typeof(T) == typeof(string)) { return (T)v.ToString(); // Cannot convert type ‘string’ to ‘T’ } else try { return (T)v; } catch (InvalidCastException) { return null; } } 如果这是某种不可饶恕的罪,请告诉我。 我用它来处理一些可能有混合类型的数据结构。

带有时区的C#字符串到DateTime

我想将字符串:“2012-04-20 10:10:00 + 0200”格式化为具有此格式的dateTime。 所以我认为它必须是“yyyy-MM-dd:mm:ss zzz”? 当我试着这个 // starttime = {20/04/2012 10:10:00} without my +0200! DateTime starttime = Convert.ToDateTime(“2012-04-20 10:10:00+0200”,CultureInfo.CurrentCulture); // And this gave me a format exception : {System.FormatException: String was not recognized as a valid DateTime. DateTime result = DateTime.ParseExact(“2012-04-20 10:10:00+0200”, “yyyy-MM-dd hh:mm:ss zzz”, CultureInfo.InvariantCulture); 由“V4Vendetta”提供的解决方案: 您应该尝试使用DateTimeOffset而不是DateTime DateTimeOffset result = DateTimeOffset.Parse(“2012-04-20 10:10:00+0200”,CultureInfo.InvariantCulture); 在这里你可以获得Offset(2小时),可以使用你的DateTime(10:10)值计算并得到你想要的输出(result.DateTime […]

为什么Char.IsDigit对于无法解析为int的字符返回true?

我经常使用Char.IsDigit检查char是否是一个数字,在LINQ查询中特别方便,以预先检查int.Parse如下所示: “123”.All(Char.IsDigit) 。 但是有些字符是数字,但不能像۵一样解析为int 。 // true bool isDigit = Char.IsDigit(‘۵’); var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures); int num; // false bool isIntForAnyCulture = cultures .Any(c => int.TryParse(‘۵’.ToString(), NumberStyles.Any, c, out num)); 这是为什么? 我的int.Parse -precheck是否通过Char.IsDigit错误? 有310个字符是数字: List digitList = Enumerable.Range(0, UInt16.MaxValue) .Select(i => Convert.ToChar(i)) .Where(c => Char.IsDigit(c)) .ToList(); 这是.NET 4(ILSpy)中Char.IsDigit的实现: public static bool IsDigit(char c) { if […]

从String获取日期

可以说我有以下字符串之一: “Hello, I’m a String… This is a Stackoverflowquestion!! Here is a Date: 16.03.2013, 02:35 and yeah, plain text blah blah..-.” “This the other string! 🙂 22.11.2012. Its a Date you see” “Here we have 2 Dates, 23.12.2012 and 14.07.2011″ 从字符串中获取这些日期的最佳和最快方法是什么(在DateTime )? (仅在字符串中出现第一个日期) 理想的回报: String 1: 16.03.2013 (as a DateTime) String 2: 22.11.2012 (” “) […]

在字符串列表中查找子字符串

我有一个这样的列表,我希望能够在此列表中搜索来自另一个字符串的子字符串。 例: List list = new List(); string srch = “There”; list.Add(“1234 – Hello”); list.Add(“4234 – There”); list.Add(“2342 – World”); 我想在我的列表中搜索”There”并返回”4234 – There” 。 我试过了: var mySearch = list.FindAll(S => s.substring(srch)); foreach(var temp in mySearch) { string result = temp; }

在某些字符计数后拆分字符串

我需要一些帮助。 我正在使用包含exception详细信息的文本文件编写错误日志。 有了这个我希望我的堆栈跟踪细节写成如下所示,而不是直线,以避免用户滚动记事本的滚动条或让我们说在第100个字符,字符串将写入下一行。 我不知道如何实现这一目标。 提前致谢。 样品(这是我目前的直线输出) STACKTRACE: at stacktraceabcdefghijklmnopqrstuvwxyztacktraceabcdefghijklmnopqrswxyztacktraceabcdefghijk * *我想要的输出(字符串将在特定字符数后写入下一行) STACKTRACE: at stacktraceabcdefghijklmno pqrstuvwxyztacktraceabcdefgh ijklmnopqrswxyztacktraceabcd efghijk 我的代码 builder.Append(String.Format(“STACKTRACE:”)); builder.AppendLine(); builder.Append(logDetails.StackTrace);

如何更改字符串的第n个元素

我在c#中有一个代码,如下所示 string s = new string(‘~’,25); int ind = 5; s[ind] = ‘A’; 它给出了一个错误 属性或索引器’string.this [int]’无法分配 – 它被读取 那么问题是什么,我该如何解决呢?

轻松翻转句子中的每个单词

例: string str = “I am going to reverse myself.”; string strrev = “I ma gniog ot esrever .flesym”; //An easy way to achieve this 因为我认为我必须遍历每个单词然后每个单词的每个字母。 我做的很好。 但我需要轻松/简短的方式。 C#代码: string str = “I am going to reverse myself.”; string strrev = “”; foreach (var word in str.Split(‘ ‘)) { string temp = “”; foreach (var […]

字符串值的通用类型

我有一个自定义类,它依赖于传递类型T来传递。 我只知道字符串forms是什么类型,因为它是如何被发送的。 我一直在寻找,但似乎无法找到我需要的确切内容。 我可以将字符串值解析为一个类型,但我需要将其解析为…某些东西,我可以作为generics参数传递。 我已经改写了我的问题,如下: // Classes structure namespace Mynamespace { public interface IRequest { } public interface IHandler where T : IRequest { void Handle(T item); } public class MyRequest : IRequest { } public class MyHandler : IHandler { void Handle(MyRequest item) { } } } // The info I get, and I […]

用有效版本替换无效字符列表(如tr)

我需要做一些梦想的.trReplace : str = str.trReplace(“áéíüñ”,”aeiu&”); 它应该改变这个字符串: a stríng with inválid charactérs 至: a string with invalid characters 我目前的想法是: str = str.Replace(“á”,”a”).Replace(“é”,”e”).Replace(“í”,”ï”… 和: sb = new StringBuilder(str) sb.Replace(“á”,”a”). sb.Replace(“é”,”e”) sb.Replace(“í”,”ï”… 但我不认为它们对于长弦有效。