如何拆分富文本框,并将数组中的值保留为整数值

本文关键字:数组 保留 整数 何拆分 拆分 文本 | 更新日期: 2023-09-27 18:34:48

首先,对不起,我的英语不好,我还在学习。现在让我们回到我的问题:-我有一个富文本框,我在其中引入了以下值:411231257805680等
-每个值都有一个新行。我的意思是,在我引入一个值后,我按 ENTER。我想要的只是,将此值保存在数组中,但作为整数元素,而不是字符。我该怎么做?我尝试使用SplitChar方法,但我不明白这种方法,因为我是C#的初学者。我的尝试:

public void ViewMyTextBoxContents(){
//Create a string array and store the contents of the Lines property.
string[] tempArray = RichTextBox1.Lines;
// Loop through the array and send the contents of the array to debug window. 
for(int counter=0; counter < tempArray.Length;counter++)
{
   System.Diagnostics.Debug.WriteLine(tempArray[counter]);
} }

但是,仍然不起作用..感谢您的帮助!!有好的一天!

如何拆分富文本框,并将数组中的值保留为整数值

int[] arr = richTextBox1.Lines.Select(x => Int32.Parse(x)).ToArray();

基本上,您缺少Int32.Parse(tempArray[counter])部分。这会将字符串解析为 int。

调整代码以执行相同的操作:

string[] tempArray = richTextBox1.Lines;
int[] resultArr = new int[tempArray.Length];
for (int counter = 0; counter < tempArray.Length; counter++)
{
    resultArr[counter] = Int32.Parse(tempArray[counter]);
}