如何比较2行并打印出“匹配”行?如果他们匹配 [英] How do I compare 2 lines and print out "they match" if they match

查看:85
本文介绍了如何比较2行并打印出“匹配”行?如果他们匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回归基础!



我刚刚开始深入挖掘数组,你能用它做什么真是太棒了(我不会告诉你我是对的吗?)





我想要完成的是一个简单的软件,它将我的文本文档中的10行文本与用户在文本框中的输入进行比较。软件。



到目前为止一直在工作和什么都没有工作。




工作原理。



我可以从文本文件中完全读取软件并将其显示在visual studio中的richTextBox上。







什么都没有用。



我似乎无法将用户输入与文本文件中的文本进行比较。





Back to basics!

I just started digging deeper into arrays and it is amazing what you can do with it (I wont have to tell you am I right?)


What I am trying to accomplish is a simple software that will compare the 10 lines of text in my Text Document with the users input in a textBox in the software.

What has been working and what has not been working so far.


What has been working.

I can make the software fully read from the textfile and display it onto a richTextBox in visual studio.



What has not been working.

I can not seem to make the users input compare to the text in the textfiles.


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\list.txt");

        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)
        {
            for (int i = 0; i < numbers.Length; i++)
            displayArrays.Text += numbers[i].ToString() + ", ";
        }
        //List Print
        private void button2_Click(object sender, EventArgs e)
         {
            for (int o = 0; o < text.Length; o++)
            {
                displayArraysString.Text += text[o].ToString();

                if(listText == displayArraysString)
                {
                    MessageBox.Show("Found a match!");
                }
            }
         }
    }
}


As you can see, the users input is matching up with the first one in the Textbox Text

<pre lang="text">https://i.gyazo.com/d9e8ed4d6845f6c67a45bbb203d45a22.png Here is a link to a image











NEW LOOP










NEW LOOP



//List Print
private void button2_Click(object sender, EventArgs e)
 {
    for (int o = 0; o < text.Length; o++)
    {
        displayArraysString.Text += text[o].ToString();

        if (listText.Text == displayArraysString.Text)
        {
            MessageBox.Show("Found a match!");

        }
        else
        {
         //Im not sure what to put here, I want it to loop to the next one and see if that one amtches
//I tried using "continue;" but that didnt work. I also tried a try catch loop but couldnt get that to work either.
        }





我尝试了什么:



我试图通过尝试将其转换为字符串来改变这一点,看看它是否改变了什么,但它没有。



What I have tried:

I've tried changing thigs up by trying to convert it to a string see if that changed anything but it didnt.

推荐答案

使用此代码...



With this code...

if (listText == displayArraysString)
{
    MessageBox.Show("Found a match!");
}





你问ListText是否与displayArraysString控制相同,答案是假 并且总是在他们引用文本框上的不同实例时。



此代码...





you are asking "Is the listText control the same control as displayArraysString" and that answer to that is "false" and always will be as they refer to different instances on textboxes.

This code...

if (listText.Text == displayArraysString.Text)
{
    MessageBox.Show("Found a match!");
}





问listText中的文本是否与displayArraysString中的文本相同这是你真正想要的要知道。



is asking "Is the text in listText the same as the text in displayArraysString" which is what you really want to know.


这篇关于如何比较2行并打印出“匹配”行?如果他们匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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