C# 在C语言中修剪部分文本文件的代码#

C# 在C语言中修剪部分文本文件的代码#,c#,.net,windows,C#,.net,Windows,我有一种情况,我收到一个文本文件,其文本格式如下: C:\Users\Admin\Documents\report2011.docx: My Report 2011 C:\Users\Admin\Documents\newposter.docx: Dinner Party Poster 08 如何修剪文本文件,以便修剪“:”及其后面的所有字符 例如,输出如下: C:\Users\Admin\Documents\report2011.docx C:\Users\Admin\Documents\n

我有一种情况,我收到一个文本文件,其文本格式如下:

C:\Users\Admin\Documents\report2011.docx: My Report 2011
C:\Users\Admin\Documents\newposter.docx: Dinner Party Poster 08
如何修剪文本文件,以便修剪“:”及其后面的所有字符

例如,输出如下:

C:\Users\Admin\Documents\report2011.docx
C:\Users\Admin\Documents\newposter.docx




Current Code:

        private void button1_Click(object sender, EventArgs e)
        {

            using (StreamWriter sw = File.AppendText(@"c:\output.txt"))
            {
                StreamReader sr = new StreamReader(@"c:\filename.txt");
                Regex reg = new Regex(@"\w\:(.(?!\:))+");
                List<string> parsedStrings = new List<string>();

                while (sr.EndOfStream)
                {
                    sw.WriteLine(reg.Match(sr.ReadLine()).Value);
                }
            }

        }
C:\Users\Admin\Documents\report2011.docx
C:\Users\Admin\Documents\newposter.docx
当前代码:
私有无效按钮1\u单击(对象发送者,事件参数e)
{
使用(StreamWriter sw=File.AppendText(@“c:\output.txt”))
{
StreamReader sr=新的StreamReader(@“c:\filename.txt”);
正则表达式reg=新正则表达式(@“\w\:(.(?!\:)+”);
List parsedStrings=new List();
while(sr.EndOfStream)
{
sw.WriteLine(reg.Match(sr.ReadLine()).Value);
}
}
}
不工作:(

编辑-根据修改后的问题添加答案。它可以稍微压缩,但为了清楚地了解正在发生的事情,可以进行扩展

 using (StreamWriter sw = File.AppendText(@"c:\output.txt"))
 {
      using(StreamReader sr = new StreamReader(@"input.txt"))
      {                    

          string myString = "";
          while (!sr.EndOfStream)
          {

                myString = sr.ReadLine();
                int index = myString.LastIndexOf(":");
                if (index > 0)
                    myString = myString.Substring(0, index);

                sw.WriteLine(myString);
           }
       }
  }

当然可以。读每一行的时候,做一个练习

Console.WriteLine(line.Substring(0,line.IndexOf(": "));
编辑

StreamReader sr=newstreamreader(“yourfile.txt”);
正则表达式reg=新正则表达式(@“\w\:(.(?!\:)+”);
List parsedStrings=new List();
而(!sr.EndOfStream)
{
添加(reg.Match(sr.ReadLine()).Value);
}
您可以使用拆分:

string[] splitted= myString.Split(':');
然后得到一个数组,在其中取第一个数组

var mySplittedString = splitted[0]
如果需要更多信息,请查看


编辑:在你的例子中,你得到一个大小至少为3的数组,所以你需要拆分[0]和[1]

我会使用这里给出的答案,找到第二个匹配项,然后在子字符串中使用它

var s = @"C:\Users\Admin\Documents\report2011.docx: My Report 2011";
var i = GetNthIndex(s,':',2);
var result = s.Substring(i+1);


public int GetNthIndex(string s, char t, int n)
{
    int count = 0;
    for (int i = 0; i < s.Length; i++)
    {
        if (s[i] == t)
        {
            count++;
            if (count == n)
            {
                return i;
            }
        }
    }
    return -1;
}
var s=@“C:\Users\Admin\Documents\report2011.docx:My Report 2011”;
var i=GetNthIndex(s',:',2);
var结果=s.子串(i+1);
public int GetNthIndex(字符串s、字符t、int n)
{
整数计数=0;
对于(int i=0;i
假设这是在文件应该存在的地方进行的,您可以在获得冒号索引后,通过检查文件是否存在来处理这一问题,同时考虑(我假设的是)描述中的任何冒号

        List<string> files = new List<string>();
        files.Add(@"C:\Users\Admin\Documents\report2011.docx: My Report 2011");
        files.Add(@"C:\Users\Admin\Documents\newposter.docx: Dinner Party Poster 08");
        files.Add(@"C:\Users\Admin\Documents\newposter.docx: Dinner Party: 08");

        int lastColon;
        string filename;
        foreach (string s in files)
        {
            bool isFilePath = false;
            filename = s;
            while (!isFilePath)
            {
                lastColon = filename.LastIndexOf(":");
                if (lastColon > 0)
                {
                    filename = filename.Substring(0, lastColon);
                    if (File.Exists(filename))
                    {
                        Console.WriteLine(filename);
                        isFilePath = true;
                    }
                }
                else
                {
                    throw new FileNotFoundException("File not found", s);
                }
            }
        }
List files=newlist();
添加(@“C:\Users\Admin\Documents\report2011.docx:MyReport2011”);
添加(@“C:\Users\Admin\Documents\newposter.docx:晚宴海报08”);
添加(@“C:\Users\Admin\Documents\newposter.docx:晚宴:08”);
结肠内;
字符串文件名;
foreach(文件中的字符串s)
{
bool isFilePath=false;
filename=s;
而(!isFilePath)
{
lastColon=filename.LastIndexOf(“:”);
如果(lastColon>0)
{
filename=filename.Substring(0,lastColon);
if(File.Exists(filename))
{
Console.WriteLine(文件名);
isFilePath=true;
}
}
其他的
{
抛出新的FileNotFoundException(“未找到文件”,s);
}
}
}
试试这个: 更快:

 string s = @"C:\Users\Admin\Documents\report2011.docx: My Report 2011";
            string path = Path.GetDirectoryName(s) + s.Split(new char[] { ':' }) [1];
            Console.WriteLine(path); //C:\Users\Admin\Documents\report2011.docx

你需要导入System.IO

以获得
C:\Temp\blah.txt:hi-mom
,输出是
C
。你的调整使其成为
C:\C
。将0更改为1。谢谢!更改为使用相同的驱动器号,以防它们在文件中更改。假设输入是
C:\Temp\blah.txt:在12:00时输入的
。然后呢?@Anthony:操作没有取消关于输入要求的详细信息足以避免所有可能的问题。@Anthony-答案基于OP的样本。如果有可能发生
12:00
,很高兴进行更改。吹毛求疵,但OPs样本显然更多地是关于第二个“:”而不是最后一个“:”。@keyboardP嗨,谢谢你的输入,你能帮我翻译一下吗在一个按钮的上下文中提供一个示例,因为我得到的输出是“字符串”my.name"在这个世界上不存在context@FlyingStreudel我认为这是因为我处理的是一个包含多个字符串的文本文件,正如您的示例所示,这里只使用一个字符串string@FlyingStrudel,我没有投你反对票,但我想这是因为你用正则表达式来解决一个简单的字符串问题。@James-把它改成从文件中读取文本是很简单的。。。我将编辑它。@jb-1000000行的一个正则表达式名义上与string.split(“:”)不同,实际上我认为它更有效。@FlyingStrudel I get:错误1找不到类型或命名空间名称“regex”(是否缺少using指令或程序集引用?)C:\Users\Limited\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 44 13 WindowsFormsApplication1您需要
使用System.Text.RegularExpressions;
在您的代码文件顶部,根据他的示例,他总能得到至少3个。
C:\path:more Text
。想更新您的帖子吗?您是对的。如果我们举个例子的话从Anthony(C:\Temp\blah.txt:在12:00输入)来看,它将是四个。因此,根据James的示例,它至少是三个。我将更新.Thx。
        List<string> files = new List<string>();
        files.Add(@"C:\Users\Admin\Documents\report2011.docx: My Report 2011");
        files.Add(@"C:\Users\Admin\Documents\newposter.docx: Dinner Party Poster 08");
        files.Add(@"C:\Users\Admin\Documents\newposter.docx: Dinner Party: 08");

        int lastColon;
        string filename;
        foreach (string s in files)
        {
            bool isFilePath = false;
            filename = s;
            while (!isFilePath)
            {
                lastColon = filename.LastIndexOf(":");
                if (lastColon > 0)
                {
                    filename = filename.Substring(0, lastColon);
                    if (File.Exists(filename))
                    {
                        Console.WriteLine(filename);
                        isFilePath = true;
                    }
                }
                else
                {
                    throw new FileNotFoundException("File not found", s);
                }
            }
        }
 string s = @"C:\Users\Admin\Documents\report2011.docx: My Report 2011";
            string path = Path.GetDirectoryName(s) + s.Split(new char[] { ':' }) [1];
            Console.WriteLine(path); //C:\Users\Admin\Documents\report2011.docx