Tag: 字符串

C#字符串格式化和填充

看起来这应该是直截了当的,但我无法做到正确。 我查看了http://idunno.org/archive/2004/14/01/122.aspx以供参考。 示例:我想打印一个double值表,每个double输出具有3个十进制精度,并占用10个空格(左对齐)。 从概念上讲,我尝试过这样的东西,但它只适用于精确的OR填充,而不是两者: foreach(line in lines) { foreach (double val in line) { Console.Write(“{0:0.000,-10}”, val); } Console.WriteLine() } 更新:我可以在非常简单的场景中使用padleft / padright,如果我有更复杂的输出它变得不是很简洁。 是否有类似于sprintf的东西?

C#排序字符串小写和大写字母

是否有标准function允许我按以下方式对大写字母和小写字母进行排序,或者我应该实现自定义比较器: student students Student Students 例如: using System; using System.Collections.Generic; namespace Dela.Mono.Examples { public class HelloWorld { public static void Main(string[] args) { List list = new List(); list.Add(“student”); list.Add(“students”); list.Add(“Student”); list.Add(“Students”); list.Sort(); for (int i=0; i < list.Count; i++) Console.WriteLine(list[i]); } } } 它将字符串排序为: student Student students Students 如果我尝试使用list.Sort(StringComparer.Ordinal) ,则排序如下: Student Students student students

为什么一些相同的字符串没有在.NET中实现?

string s1 = “test”; string s5 = s1.Substring(0, 3)+”t”; string s6 = s1.Substring(0,4)+””; Console.WriteLine(“{0} “, object.ReferenceEquals(s1, s5)); //False Console.WriteLine(“{0} “, object.ReferenceEquals(s1, s6)); //True 字符串s5和s6都具有与s1相同的值(“test”)。 基于字符串实习概念,这两个语句必须已评估为true。 有人可以解释为什么s5没有与s1相同的参考?

搜索列表表示字符串.StartsWith()

我有一个 List 有1500个字符串。 我现在使用以下代码只提取以字符串prefixText开头的字符串。 foreach(string a in ) { if(a.StartsWith(prefixText, true, null)) { newlist.Add(a); } } 这很快,但我正在寻找谷歌快速。 现在我的问题是,如果我按字母顺序排列List,那么比较char by char我可以加快速度吗? 或者其他任何有关加快速度的建议?

在C#中查找字符串中的所有模式索引

如何使用c#查找字符串中模式的所有索引? 例如,我想在字符串中找到所有##模式索引,如45##78$$#56$$JK01UU

如何在C#中拆分字符串

我有一个字符串 “List_1 fooo asdf List_2 bar fdsa XList_3 fooo bar” 和List一样 List l_lstValues = new List { “List_1”, “XList_3”, “List_2” }; 我需要根据l_lstValues的值拆分字符串。 所以分裂的子串就像 List_1 fooo asdf List_2 bar fdsa XList_3 fooo bar 请给我一个方法来做到这一点,提前谢谢

如何获得带空格和混合大小写的随机字符串?

我需要生成一个包含空格和mixedCase的随机字符串。 这就是我到目前为止所做的一切: /// /// The Typing monkey generates random strings – can’t be static ’cause it’s a monkey. /// /// /// If you wait long enough it will eventually produce Shakespeare. /// class TypingMonkey { /// /// The Typing Monkey Generates a random string with the given length. /// /// Size of the string /// […]

查看.NET框架中的EqualsHelper方法

我正在查看.NET框架的String类的Equals方法实现,发现它使用EqualsHelper方法。 我发现它确实是一种非常酷且有效的方法,但是我发现了一些非常有用的东西,为什么它们通过除法运算来增加指针(或产生偏移),如: *(long*)(ptr + (IntPtr)8 / 2), ptr += (IntPtr)4 / 2; 等等。 谢谢!

解析包含数组的字符串

我想将包含递归字符串数组的字符串转换为深度为一的数组。 例: StringToArray(“[a, b, [c, [d, e]], f, [g, h], i]”) == [“a”, “b”, “[c, [d, e]]”, “f”, “[g, h]”, “i”] 看起来很简单。 但是,我来自function背景,我不熟悉.NET Framework标准库,所以每次(我从头开始像3次)我最终只是简单的丑陋代码。 我最近的实施就在这里 。 如你所见,这很丑陋。 那么,C#的方法是什么呢?

最有效的连接字符串的方式

我需要连接很多字符串并在其中任何一个之间加上逗号。 我有一个字符串列表 “123123123213” “1232113213213” “123213123” 而且我想得到 “123123123213,1232113213213,123213123” 我想知道实现这一目标的最佳方法是什么。 我可以这样做: private List stringList = new List { // a lot of strings in here “1234567890”, “34343434”, “4343434” }; string outcome = string.Join(“,”, stringList.ToArray()); 或者可能: StringBuilder builder = new StringBuilder(); stringList.ForEach(val => { builder.Append(val); builder.Append(“,”); }); string outcome = builder.ToString(); 哪种方式更好? 你知道连接字符串的更好方法吗?