有人可以帮我简化一些行 [英] Could someone help me simplify some lines

查看:118
本文介绍了有人可以帮我简化一些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!首先,我要感谢帮助我学习和理解我一直在问的所有问题的每一个人。




现在..我有这个我正在研究的软件(它正在工作)而且非常简单,我理解85%其余的我得到了帮助,但他没有很好地解释它。 />




我需要帮助的是简化一些代码(Dummy it down)并解释它的含义。一切都有评论//这个我真的不明白,所以如果可能的话,有人可以评论出来解释它为什么以及它到底是什么。



谢谢非常! :D $ / $








 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;

命名空间数组
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
int [] numbers = new int [ 5 ];
List< int> numbersList = new List< int>();
string text = System.IO.File.ReadAllText( @ C:\ Users \Developer\Documents\Visual Studio 2015 \Projects\Arrays\hash.txt);
string [] displayStringArrays = null ; //

私有 void Form1_Load( object sender,EventArgs e)
{
// numbers [0] = 12;
// 数字[1] = 10;
// numbers [2] = 25;
// 数字[3] = 10;
// numbers [4] = 15;
// numbersList.Add(23);
// numbersList.Add(32);
// numbersList.Add(35);
}
< span class =code-comment> // 数组打印
private void button1_Click( object sender,EventArgs e)
{
displayArrays.Text = listText.Text;
}
// 列表打印
private void button2_Click( object sender,EventArgs e)
{
displayStringArrays = text.Split(' \ n')。ToArray() ; //
foreach var item in displayStringArrays) //
{
displayArraysString.Text + = item;

if (listText.Text == item.Substring( 0 ,item .Length - 1 )|| listText.Text == item) //
{
MessageBox.Show( 找到匹配项!);
return ;
}
其他
{
// < span class =code-comment> Something。

}
}

}

private void displayArraysString_TextChanged( object sender,EventArgs e)
{

}
}
}





我的尝试:



我试过googleing但没有找到简单的答案

解决方案

< pre lang =C#> string [] displayStringArrays = null ;



声明一个名为 displayStringArrays 的字符串数组,并将数组的值初始化为 null - 即它已经声明但尚无价值。 = null 并非真的有必要。

 displayStringArrays = text.Split('  \ n')。ToArray(); 



此行将获取变量 text 的内容,并将其拆分为单独的字符串,只要它找到回车符/换行('\ n'')。它将每个字符串放入之前声明的数组中。

  foreach  var  item   displayStringArrays)

正在说 - 执行以下语句块(在匹配的括号之间) {和})一次为数组中的每个字符串。它类似于使用

   int  i =  0 ; i <  displayStrings.Length; i ++)



< pre lang =C#> if (listText.Text == item.Substring( 0 ,item。长度 - 1 )|| listText.Text == item)

这是比较 Text 名为 listText 的控件的属性,该字符串当前是 foreach 循环的主题 - 它将是逐个检查数组中的所有字符串。比较是检查数组中的字符串是否与控件中的文本完全匹配( || ),如果控件中的文本以与文本相同的文本开头string( Substring


阅读这些



< a href =https://msdn.microsoft.com/en-gb/library/aa288453(v=vs.71).aspx>数组教程(C#) [ ^ ]

String.Split Method(System) [< a href =https://msdn.microsoft.com/en-us/library/system.string.split(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]

String.Substring Method(Int32 ,Int32)(系统) [ ^ ]



  string  [] displayStringArrays =  null ; 





这意味着displayStringArrays是一个字符串数组但是,它只是在这一点上被定义,它实际上并不包含一个字符串数组。另外需要注意的是,此时我们还不知道数组的大小,我们只知道此变量现在可以包含将来的字符串数组。



 displayStringArrays = text.Split('  \\\
')ToArray的(); //





首先关闭上面的代码不需要ToArray



 displayStringArrays = text.Split('  \ n');  //   





Split使用您提供的字符作为分隔符将单个字符串转换为字符串数组。因此,如果文本是abcabcabcdef,则该文本上的Split('c')将生成这样的数组



 displayStringArrays [  0 ] =   ab; 
displayStringArrays [ 1 ] = ab ;
displayStringArrays [ 2 ] = ab ;
displayStringArrays [ 3 ] = def ;





因此,当您拆分('\ n')时,\ n是新行的代码,你正在获取一大堆具有多行的文本并创建一个数组,其中该数组中的每个元素都是一行。所以,如果文字是;



现在如何
褐牛





你有一个类似的数组



 displayStringArrays [ 0 ] =  现在怎么样; 
displayStringArrays [ 1 ] = 褐牛;





  foreach  var  item  in  displayStringArrays)





该行将导致{}块中的代码为数组中的每个项循环一次,因此第一次迭代item将是displayStringArrays [0],第二次迭代将是displayStringArrays [1]等等。



基本上代码正在做的是将文件中的文本拆分成一个行数组,并一次处理每一行该行在item中。



  if (listText。 Text == item.Substring( 0 ,item.Length  -   1 )|| listText.Text ==项目)





这不是很好的代码。 ||表示或,因此如果左侧或右侧语句或右侧语句为真,则if评估为真。右边的语句(listText.Text == item)很简单,这意味着该文本框中的文本与item相同。



代码在左边是将文本框与item的子字符串0,length-1进行比较。这意味着从字符0(第一个)开始,然后到项目减去1的长度,即第二个但最后一个字符。所以如果item是你好那么那个子串是



地狱



长度Hello是5,所以从第一个字符开始,然后取4个字符就可以获得Hell



所以它的项目是Hello,listText.Text是地狱+<任何角色>然后那将解决为真。所以如果listText是其中任何一个;



海拉

Hellb

Hellc

Helld

Helle



然后



 listText.Text == item.Substring( 0 ,item.Length  -   1 





将解析为true。我认为逻辑是出于某种原因而完成的,我将进一步假设它捕获一个尾随空格,因此尾随回车。由于我们无法看到您的数据,我们不知道,但这是我的猜测。如果那是正确的那么有更好的方法来处理,例如将拆分更改为此



 displayStringArrays = text.Split(< span class =code-keyword> new   char  [] {'  \ r''  \ n'} ,StringSplitOptions.RemoveEmptyEntries); 





如果代码在那里忽略尾随空格那么你可以使用Trim()



  if (listText.Text.Trim()== item.Trim()) 





因为代码引入了一个会产生意想不到的结果的错误。例如该行可能是



修补



你的比较文字可能是男人和当它不是时,它将注册一个匹配。


Hello! First of all I would like to thank every single one of you who has helped me so far learning and understanding all the questions I've been asking.


Now.. I have this software that I am working on (its working) and its really simple, well I understand 85% of it the rest I got help with but he didnt explain it very well..


What I need help with is simplify some code (Dummy it down) and explain what it means. Everything with the comment //This I dont really understand, so if possible, could someone comment it out to explain why its there and what it exactly does.

Thank you so much! :D




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Arrays
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int[] numbers = new int[5];
        List<int> numbersList = new List<int>();
        string text = System.IO.File.ReadAllText(@"C:\Users\Developer\Documents\Visual Studio 2015\Projects\Arrays\hash.txt");
        string[] displayStringArrays = null; //This

        private void Form1_Load(object sender, EventArgs e)
        {
            //numbers[0] = 12;
            //numbers[1] = 10;
            //numbers[2] = 25;
            //numbers[3] = 10;
            //numbers[4] = 15;
            //numbersList.Add(23);
            //numbersList.Add(32);
            //numbersList.Add(35);
        }
        //Array Print
        private void button1_Click(object sender, EventArgs e)
        {
            displayArrays.Text = listText.Text;
        }
        //List Print
        private void button2_Click(object sender, EventArgs e)
        {
            displayStringArrays = text.Split('\n').ToArray(); //This
            foreach (var item in displayStringArrays) //This
            {
                displayArraysString.Text += item;

                if (listText.Text == item.Substring(0, item.Length - 1) || listText.Text == item) //This
                {
                    MessageBox.Show("Found a match!");
                    return;
                }
                else
                {
                    //Something.
                }
            }

        }

        private void displayArraysString_TextChanged(object sender, EventArgs e)
        {

        }
    }
}



What I have tried:

I've tried googleing but no "simple" answers were found

解决方案

string[] displayStringArrays = null;


Is declaring a array of strings called displayStringArrays and initialising the value of the array to null - i.e. it has been declared but has no value (yet). The = null is not really necessary.

displayStringArrays = text.Split('\n').ToArray(); 


This line is taking the contents of the variable text and splitting it into separate strings wherever it finds a carriage return/line feed ('\n'). It places each of the strings into the array that was declared earlier.

foreach (var item in displayStringArrays)

Is saying - execute the following block of statements (between the matching braces { and }) once for each one of the strings in the array. It's similar to using

for (int i =0; i < displayStrings.Length; i++)


if (listText.Text == item.Substring(0, item.Length - 1) || listText.Text == item)

This is comparing the Text property of a control called listTextto the string that is currently the subject of the foreach loop - it will be checked against all of the strings in the array one by one. The comparison is checking to see if the string in the array exactly matches the text in the control OR (||) if the text in the control starts with the same text as the string (Substring)


Have a read of these

Arrays Tutorial (C#)[^]
String.Split Method (System)[^]
String.Substring Method (Int32, Int32) (System)[^]

string[] displayStringArrays = null;



This means that displayStringArrays is an array of strings, however it is simply being defined at this point, it doesn't actually contain an array of strings just yet. The other thing of note is that at this point we don't know the size of the array either, we just know that this variable can now contain an array of strings in the future.

displayStringArrays = text.Split('\n').ToArray(); //This



First off the code above doesn't need the ToArray

displayStringArrays = text.Split('\n'); //This



Split converts a single string into an array of strings using the character you supply as the separator. So if text was "abcabcabcdef" then Split('c') on that text would result in an array like this

displayStringArrays[0] = "ab";
displayStringArrays[1] = "ab";
displayStringArrays[2] = "ab";
displayStringArrays[3] = "def";



So when you Split('\n'), \n is the code for new line, you are taking a chunk of text that has multiple lines and creating an array where each element in that array is a line. So if the text was;

how now
brown cow



you'd have an array like

displayStringArrays[0] = "how now";
displayStringArrays[1] = "brown cow";



foreach (var item in displayStringArrays)



That line will cause the code in the { } block to loop once for each item in your array, so the first iteration "item" will be displayStringArrays[0], the second iteration will be displayStringArrays[1] and so on.

Basically what the code is doing is splitting the text in the file into an array of lines, and processing each line at a time with that line being in "item".

if (listText.Text == item.Substring(0, item.Length - 1) || listText.Text == item)



That's not good code. The "||" means "or", so if the statement to the left or it or the statement to the right is true the "if" evaluates as true. The statement to the right (listText.Text == item) is straightforward, it means if the text in that textbox is the same as "item".

The code to the left is comparing the text box with the substring of "0, length-1" of "item". That means starting at character 0 (the first) and going to the length of item minus 1, ie the second but last character. So if "item" is "Hello" then that substring is

"Hell"

The length of Hello is 5, so starting at the first character and taking the 4 characters you get "Hell"

So it item is "Hello" and listText.Text is "Hell" + <any character> then that will resolve to true. So if listText is any of these;

Hella
Hellb
Hellc
Helld
Helle

then

listText.Text == item.Substring(0, item.Length - 1)



will resolve to true. I assume that logic was done for a reason and I am going to further assume it is to capture either a trailing space, so a trailing carriage return. As we can't see your data we don't know, but that is my guess. If that Is right then there are better ways of handling that, eg changing the Split to this

displayStringArrays = text.Split(new char[]{'\r','\n'}, StringSplitOptions.RemoveEmptyEntries);



If the code is there to ignore trailing spaces then you can use Trim()

if (listText.Text.Trim() == item.Trim())



As it is the code has introduced a bug that is going to give unintended results. Eg the line might be

"Mend"

and your compare text might be "Men" and that is going to register a match when it isn't.


这篇关于有人可以帮我简化一些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆