Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在文本文件c中搜索句子#_C# - Fatal编程技术网

C# 在文本文件c中搜索句子#

C# 在文本文件c中搜索句子#,c#,C#,我想做一个程序来搜索一个句子,但我的程序只能搜索一个字符,不能搜索整个句子。我不想使用Contains。那么从这里我应该编辑代码的哪一部分?我想我的循环有问题,但我想不出来 class Readfile { public void readf() { int j; int i = 0; int flag = 0; Console.WriteLine("Enter sentence : "); stri

我想做一个程序来搜索一个句子,但我的程序只能搜索一个字符,不能搜索整个句子。我不想使用
Contains
。那么从这里我应该编辑代码的哪一部分?我想我的循环有问题,但我想不出来

class Readfile
{
    public void readf()
    {
        int j;
        int i = 0;
        int flag = 0;
        Console.WriteLine("Enter sentence : ");
        string str = Console.ReadLine();
        char[] c1 = str.ToCharArray();
        string filename = @"C: \Users\jan31\Desktop\matthew\text.txt";
        using (StreamReader sr = new StreamReader(filename))
        {
            string str2 = sr.ReadToEnd();
            char[] fs = str2.ToCharArray();
            for (i = 0; i < fs.Length; i++) //loop for file
            {
                if (flag == c1.Length)
                {
                    Console.WriteLine("found");
                    break;
                }
                else
                {
                    for (j = 0; j < c1.Length;) //loop for user string input
                    {

                        if (c1[j] == fs[i])
                        {
                            flag = flag + 1;
                            j++;
                            break;
                        }
                        else
                        {
                            flag = 0;
                            j = 0;
                            break;
                        }

                    }
                }

            }
        }
    }
}
类读取文件
{
公共无效readf()
{
int j;
int i=0;
int标志=0;
Console.WriteLine(“输入句子:”);
string str=Console.ReadLine();
char[]c1=str.ToCharArray();
字符串文件名=@“C:\Users\jan31\Desktop\matthew\text.txt”;
使用(StreamReader sr=新StreamReader(文件名))
{
字符串str2=sr.ReadToEnd();
char[]fs=str2.ToCharArray();
for(i=0;i
在第33行中,删除
break
语句并添加
i++
。我验证了它的工作原理。问题是,当第一个字符匹配时,您没有继续搜索


我建议你把这篇文章移到一个更适合代码审查的地方。

在第33行,删除
break
语句并添加
I++
。我验证了它的工作原理。问题是,当第一个字符匹配时,您没有继续搜索


我建议你把这篇文章移到一个更适合代码评审的地方。

是的,你的循环不正确。请看下面我的解决方案:

        for (i = 0; i < fs.Length; i++)
        {
            if (c1[0] == fs[i])
            {
                for (var j = 1; j < c1.Length;) //loop for user string input
                {
                    if (c1[j] != fs[i = j])
                    {
                        break;
                    }
                }

                Console.WriteLine("found");
                Console.ReadLine();
                return;
            }
        }

        Console.WriteLine("not found");
        Console.ReadLine();
for(i=0;i
是的,您的循环不正确。请看下面我的解决方案:

        for (i = 0; i < fs.Length; i++)
        {
            if (c1[0] == fs[i])
            {
                for (var j = 1; j < c1.Length;) //loop for user string input
                {
                    if (c1[j] != fs[i = j])
                    {
                        break;
                    }
                }

                Console.WriteLine("found");
                Console.ReadLine();
                return;
            }
        }

        Console.WriteLine("not found");
        Console.ReadLine();
for(i=0;i
因此,如果要读取文件并在读取时查找字符串。也许你的文件太大了,不能马上读。这是我的解决办法

    class Readfile
    {
        public static void readf()
        {
            Console.WriteLine("Enter sentence : ");
            string str = Console.ReadLine();
            string filename = @"C: \Users\jan31\Desktop\matthew\text.txt";
            Encoding fileEncoding = Encoding.Default;
            bool Found = false;
            using (Stream s = System.IO.File.OpenRead(filename))
            {
                int current;
                string ReadText = "";
                List<Byte> L = new List<Byte>();
                do
                {
                    current = s.ReadByte();
                    if (current != -1)
                    {
                        L.Add((Byte)current);
                        ReadText = fileEncoding.GetString(L.ToArray());
                        if (ReadText.Length > str.Length)
                        {
                            L.RemoveAt(0);
                            ReadText = fileEncoding.GetString(L.ToArray());
                        }
                        if (ReadText.Length == str.Length)
                        {
                            if (ReadText == str)
                            {
                                //Found it ##############
                                Found = true;
                            }
                        }
                    }
                } while ((current != -1) && !Found);
            }
            if (!Found) {
                //Not Found it ##############
            }
        }
    }
类读取文件
{
公共静态void readf()
{
Console.WriteLine(“输入句子:”);
string str=Console.ReadLine();
字符串文件名=@“C:\Users\jan31\Desktop\matthew\text.txt”;
Encoding fileEncoding=Encoding.Default;
bool-Found=false;
使用(streams=System.IO.File.OpenRead(文件名))
{
电流;
字符串ReadText=“”;
列表L=新列表();
做
{
当前=s.ReadByte();
如果(当前!=-1)
{
L.添加((字节)当前值);
ReadText=fileEncoding.GetString(L.ToArray());
如果(ReadText.Length>str.Length)
{
L.RemoveAt(0);
ReadText=fileEncoding.GetString(L.ToArray());
}
if(ReadText.Length==str.Length)
{
如果(ReadText==str)
{
//找到了##############
发现=真;
}
}
}
}而((当前!=-1)&&!找到);
}
如果(!找到){
//没找到##############
}
}
}

因此,如果要读取文件并在读取时查找字符串。也许你的文件太大了,不能马上读。这是我的解决办法

    class Readfile
    {
        public static void readf()
        {
            Console.WriteLine("Enter sentence : ");
            string str = Console.ReadLine();
            string filename = @"C: \Users\jan31\Desktop\matthew\text.txt";
            Encoding fileEncoding = Encoding.Default;
            bool Found = false;
            using (Stream s = System.IO.File.OpenRead(filename))
            {
                int current;
                string ReadText = "";
                List<Byte> L = new List<Byte>();
                do
                {
                    current = s.ReadByte();
                    if (current != -1)
                    {
                        L.Add((Byte)current);
                        ReadText = fileEncoding.GetString(L.ToArray());
                        if (ReadText.Length > str.Length)
                        {
                            L.RemoveAt(0);
                            ReadText = fileEncoding.GetString(L.ToArray());
                        }
                        if (ReadText.Length == str.Length)
                        {
                            if (ReadText == str)
                            {
                                //Found it ##############
                                Found = true;
                            }
                        }
                    }
                } while ((current != -1) && !Found);
            }
            if (!Found) {
                //Not Found it ##############
            }
        }
    }
类读取文件
{
公共静态void readf()
{
Console.WriteLine(“输入句子:”);
string str=Console.ReadLine();
字符串文件名=@“C:\Users\jan31\Desktop\matthew\text.txt”;
Encoding fileEncoding=Encoding.Default;
bool-Found=false;
使用(streams=System.IO.File.OpenRead(文件名))
{
电流;
字符串ReadText=“”;
列表L=新列表();
做
{
当前=s.ReadByte();
如果(当前!=-1)
{
L.添加((字节)当前值);