所以基本上我要做的是一个控制台程序,当你插入一个字符串(一个文件的路径为ex:C:\ Text.txt)时,它会返回带扩展名的完整路径(这个部分工作)然后它将返回该文件的内容,然后要求您插入一个字符在该Text.txt文件中搜索。从文本文件读取的部分工作昨天,但现在不再工作,也是字符搜索不起作用。我想要做的是基本上搜索该文件中的特定字符(例如字符'a'(从键盘插入),然后计算它在Text.txt中的次数因为某些原因,我无法显示Text.txt文件的内容,并且出现次数也不起作用。请帮助我,我花了很多时间在这上面,它不起作用:(。Ty
以下是整个代码:
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class GetStatistics
{
public String GetFileDetails(String PathS)
{
String Space = " ";
Console.WriteLine("Absolute: " + Path.IsPathRooted(PathS));
Console.WriteLine("Root: " + Path.GetPathRoot(PathS));
Console.WriteLine("Directory: " + Path.GetDirectoryName(PathS));
Console.WriteLine("File name: " + Path.GetFileName(PathS));
if (Path.HasExtension(PathS))
Console.WriteLine("Extension: " + Path.GetExtension(PathS));
Console.WriteLine("Full path: " + PathS);
return Space;
}
public String ReadFromFile(String PathS)
{
String Extra = "\n***************************\n";
Console.WriteLine(Extra);
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(PathS);
while ((line = file.ReadLine()) != null)
{
Console.WriteLine(line);
counter++;
}
String CharacterSearch;
CharacterSearch = Console.ReadLine();
var count = File.ReadLines(PathS)
.Select((c, i) => new { Character = c, Index = i })
.ToList()
.Where(x => x.Character == CharacterSearch);
file.Close();
return Extra;
}
}
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Introduceti un path pentru fisierul text care vreti sa il deschideti: ");
String PathS = Console.ReadLine();
GetStatistics G = new GetStatistics();
Console.WriteLine(G.GetFileDetails(PathS));
Console.WriteLine(G.ReadFromFile(PathS));
}
}
}
问题在于ReadFromFile。
答案 0 :(得分:1)
x.Character == CharacterSearch
它与其中一部分的字符串不匹配,因为我与整行匹配。
如果您检查它是否包含在该行中。
应该是
x.Character.Contains(CharacterSearch)
匹配次数(IEnumerable <String>
)也是count.Count()
我认为显示我需要的跟踪号的部分。
Console.WriteLine("count:{0}", count.Count());
注意:这不是字符串出现的次数,因为它是匹配的行。
用于计算字符
String CharacterSearch;
CharacterSearch = Console.ReadLine();
var count = File.ReadLines(PathS)
.Select((String s) => { return s.Length - s.Replace(CharacterSearch, "").Length; })
.Sum();
file.Close();
Console.WriteLine("count:{0}", count);
return Extra;
单词和字符
.Select((s) => { return (s.Length - s.Replace(CharacterSearch, "").Length)/CharacterSearch.Length; })
答案 1 :(得分:0)
基本上你没有返回字符搜索计数的计数
试一试:添加此方法
int getCharCount(String PathS)
{
Console.WriteLine("Enter Char to be searched!");
char CharacterSearch = Console.ReadKey().KeyChar;
Console.ReadLine();
int count = 0;
String content=File.ReadAllText(PathS);
for (int i = 0; i < content.Length; i++)
{
if (content[i].ToString().ToLower().Equals(CharacterSearch.ToString().ToLower()))
count++;
}
return count;
}
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Introduceti un path pentru fisierul text care vreti sa il deschideti: ");
String PathS = Console.ReadLine();
GetStatistics G = new GetStatistics();
Console.WriteLine(G.GetFileDetails(PathS));
Console.WriteLine(G.ReadFromFile(PathS));
Console.WriteLine("Serch Count ="+G.getCharCount(PathS));
}
}