Tag: string

令人惊讶的子串行为

我在使用Substring方法时遇到了这种行为: static void Main(string[] args) { string test = “123”; for (int i = 0; true; i++) { try { Console.WriteLine(“\”{0}\”.Substring({1}) is \”{2}\””, test, i, test.Substring(i)); } catch (ArgumentOutOfRangeException e) { Console.WriteLine(“\”{0}\”.Substring({1}) threw an exception.”, test, i); break; } } } 输出: “123”.Substring(0) is “123” “123”.Substring(1) is “23” “123”.Substring(2) is “3” “123”.Substring(3) is “” “123”.Substring(4) […]

c#运行命令行中的字符串格式

如果我在控制台中运行以下命令,它将成功: “C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app (1)\fiji.exe” -macro “C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app (1)\macros\FFTBatch.ijm” C:\Users\myAccount.Unit\Documents\Untitled001\ 其中”C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app (1)\fiji.exe”是应用程序文件, “C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app (1)\macros\FFTBatch.ijm”是执行的宏文件, 和C:\Users\myAccount.Unit\Documents\Untitled001\是前一个宏处理的图像。 但是,当我使用C#来完成这项工作时,它失败了(意味着根本没有响应)。 以下是相关代码: string _fijiExeFile = “C:\\Users\\myAccount.Unit\\Favorites\\Downloads\\fiji.app (1)\\fiji.exe”; string _ijmFile = “C:\\Users\\myAccount.Unit\\Favorites\\Downloads\\fiji.app (1)\\macros\\FFTBatch.ijm”; string _inputDir = “C:\\Users\\myAccount.Unit\\Documents\\Untitled001\\”; string fijiCmdText = string.Format(“/C \”{0}\” -macro \”{1}\” {2}”, _fijiExeFile, _ijmFile, _inputDir); try { System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName […]

如何从字符串c#中删除\ r \ n

我试图找出一个简单的方法从字符串中删除\ r \ n。 示例:text =“this.is.a.string。\ r \ n \ nthis.is.a.string \ r \ n” 我试过了: text.Replace(“\r\n”, “”)和text.Replace(“\r\n”, string.Empty)但它不起作用。 \r\n仍然在字符串中…… 结果应为:“this.is.a.string.this.is.a.string”

C#中子字符串的意外行为

.net System.String类中的Substring()方法的定义是这样的 public string Substring(int startIndex) 其中startIndex是根据方法定义的“此实例中子字符串从零开始的起始字符位置” 。 如果我理解正确,这意味着它将给我一部分字符串,从给定的从零开始的索引开始。 现在,如果我有一个字符串”ABC”并使用不同索引的子字符串,我得到以下结果。 var str = “ABC”; var chars = str.ToArray(); //returns 3 char ‘A’, ‘B’, ‘C’ as expected var sub2 = str.Substring(2); //[1] returns “C” as expected var sub3 = str.Substring(3); //[2] returns “” …!!! Why no exception?? var sub4 = str.Substring(4); //[3] throws ArgumentOutOfRangeException as expected […]

字符串未被识别为有效的DateTimeexception。

string datestring = txtNewReminderRemindDate.Text.ToString() + ” ” + RemTime.ToString(); 所以我的约会时间是“17/5/2017 19:10:00” 我正在尝试将此字符串转换为我的通知。 但是当我这样做时: DateTime alarm = DateTime.ParseExact(datestring, “dd/MM/yyyy HH:mm:ss”, CultureInfo.InvariantCulture); 我明白了: Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime. 我不明白我做错了什么。 我甚至尝试做: DateTime alarm= DateTime.Parse(datestring);

C#从运行时创建的文本框中获取文本

您好我正在制作一个包含2个文本框和2个按钮的程序当我按下添加按钮时,它将使用此代码创建2个新文本框: private void ADD_ROW_Click(object sender, EventArgs e) { //Make the NEW_TEXTBOX_1 HOW_FAR += 1; TextBox NEW_TEXTBOX_1 = new TextBox(); NEW_TEXTBOX_1.Name = “NAME_TEXTBOX_” + HOW_FAR.ToString(); //Set NEW_TEXTBOX_1 font NEW_TEXTBOX_1.Font = new Font(“Segoe Print”, 9); NEW_TEXTBOX_1.Font = new Font(NEW_TEXTBOX_1.Font, FontStyle.Bold); //Set pos and size and then create it. NEW_TEXTBOX_1.Location = new System.Drawing.Point(16, 71 + (35 * HOW_FAR)); […]

如何`.Take()`对一个字符串并在结尾处获取一个字符串?

LINQ to Objects支持对字符串对象的查询,但是当我使用如下代码时: string SomeText = “this is some text in a string”; return SomeText.Take(6).ToString(); 我得到的只是: System.Linq.Enumerable+d__3a`1[System.Char] 在这个问题中 ,这被视为“意外”,但这是我实际上要做的事情,我无法通过任何地方的搜索找到它。 我知道还有其他方法可以操作字符串,但后来我也知道你可以用LINQ做一些非常酷的技巧,我想知道是否有办法用LINQ修剪字符串到一定的长度?

使用C#进行字符串连接

我有一个输入字符串: “风险管理,投资组合管理,投资计划” 如何将此字符串转换为: “风险管理”+“投资组合管理”+“投资计划” 谢谢。

在C#中用\替换\\

我有一个带有双反斜杠的长字符串(路径),我想用单个反斜杠替换它: string a = “a\\b\\c\\d”; string b = a.Replace(@”\\”, @”\”); 这段代码什么都不做…… b仍然是”a\\b\\c\\d” 我也尝试了不同的反斜杠组合,而不是使用@ ,但没有运气。

如何使用C#在数字子字符串上排序字符串列表

我有一个字符串列表,每个字符串包含一个数字子字符串,我想根据该子字符串的数值重新排序。 该集合看起来像这样,但更大: List strings= new List { “some-name-(1).jpg”, “some-name-(5).jpg”, “some-name-(5.1).jpg”, “some-name-(6).jpg”, “some-name-(12).jpg” }; 数字将始终用括号括起来,括号是字符串中唯一的括号,因此使用String.IndexOf是可靠的。 请注意,不仅可能存在缺失的数字,而且还可能存在小数,而不仅仅是整数。 我很难获得在该子字符串的数值上排序的那些相同字符串的重新排序列表。 有没有人有办法做到这一点,希望有一个表现良好的方法? 谢谢。