在C#中的.text文件中从单行中获取多个子串

问题描述 投票:1回答:3

我正在处理的课程作业的一部分要求我从.txt文件中隔离“学生”信息的某些部分,例如姓氏,名字和gpa。虽然我可以得到正确显示的姓氏,但不仅如此,名字将被切断或者看似随机地转到中间名字。我需要帮助,根据线条中的标记(撇号)获得一致的截断点。

这是.txt文件Students2的示例,它位于程序的Bin Debug中

(LIST (LIST 'Abbott 'Ashley 'J ) '8697387888 '[email protected] 2.3073320999676614 )
(LIST (LIST 'Abbott 'Bradley 'M ) 'NONE '[email protected] 3.1915725161177115 )
(LIST (LIST 'Abbott 'Ryan 'T ) '8698689793 '[email protected] 3.448215586562192 )
(LIST (LIST 'Abel 'Heather 'M ) '8698689386 '[email protected] 3.2517764202656974 )

希望我对标记的意义​​是有道理的,如果有人想知道信息是假的。

以下是我当前的代码,其中一些是在一个示例中提供的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication61
{
class Program
{
    static void Main(string[] args)
    {

        StreamReader inFile;
        string inLine;

        if (File.Exists("Students2.txt"))
        {
            try
            {



                using (StreamWriter Malachi = File.AppendText("Students2.txt"))
                {
                    Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 '[email protected] 4.0000000000000000 )");
                }

                inFile = new StreamReader("Students2.txt");
                while ((inLine = inFile.ReadLine()) != null)
                {
                    int start = inLine.IndexOf("'");
                    if (start >= 0)
                    {
                        inLine = inLine.Substring(start);
                        int end = inLine.IndexOf(" ");
                        string lastname = inLine.Substring(0, end);
                        int endTwo = inLine.IndexOf(" ");
                        string firstname = inLine.Substring(end, endTwo);
                        int endThree = inLine.IndexOf(" ");
                        string email = inLine.Substring(endTwo, endThree);
                        Console.WriteLine( lastname + firstname );

                    }
                }
            }


            catch (System.IO.IOException exc)
            {
                Console.WriteLine("Error");
            }
        }
    }
}
}

我想再次知道我在切断特定点时做错了什么。非常感谢任何和所有的帮助。

c# substring
3个回答
2
投票

你可以使用split函数和replace函数来获得你想要的东西

    string test = "(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 '[email protected] 4.0000000000000000 )";

    test = test.Replace(")","");

    string[] abc = test.Split(new string[] { "'" }, StringSplitOptions.None);

    Console.WriteLine("Last Name =" + abc[1]);
    Console.WriteLine("First Name =" + abc[2]);
    Console.WriteLine("Middle Initial =" + abc[3]);
    Console.WriteLine("Email gpa =" + abc[abc.Length-1]);

UPDATE

如果gpa没有撇号,你可以得到如下的那些值,只需用这个替换最后一行

    Console.WriteLine("Email =" + (abc[abc.Length-1]).Split(' ')[0]);
    Console.WriteLine("gpa =" + (abc[abc.Length-1]).Split(' ')[1]);

这是https://dotnetfiddle.net/vJYLXW的小提琴


1
投票

你每次脱掉子串就需要更新inLine -

int start = inLine.IndexOf("'");
                    if (start >= 0)
                    {
                        inLine = inLine.Substring(start);
                        int end = inLine.IndexOf(" ");
                        string lastname = inLine.Substring(0, end);
                        inLine = inLine.Substring(end + 1);
                        int endTwo = inLine.IndexOf(" ");
                        string firstname = inLine.Substring(end, endTwo);
                        inLine = inLine.Substring(endTwo + 1);
                        int endThree = inLine.IndexOf(" ");
                        string email = inLine.Substring(endTwo, endThree);
                        .
                        .

                    }

等等.....

或者你在第一个字符串的结束点之后找到下一个空格,如下所示 -

int start = inLine.IndexOf("'");
            if (start >= 0)
            {
                inLine = inLine.Substring(start);
                int end = inLine.IndexOf(" ");
                string lastname = inLine.Substring(0, end);
                int endTwo = inLine.IndexOf(' ', end + 1);
                string firstname = inLine.Substring(end, endTwo - end);
                int endThree = inLine.IndexOf(' ', endTwo + 1);
                string middleinitial = inLine.Substring(endTwo, endThree - endTwo);
                endThree += 2; // to escape ')' after middle initial
                int endFour = inLine.IndexOf(' ', endThree + 1);
                string phone = inLine.Substring(endThree, endFour - endThree);
                int endFive = inLine.IndexOf(' ', endFour + 1);
                string email = inLine.Substring(endFour, endFive - endFour);
                int endSix = inLine.IndexOf(' ', endFive + 1);
                string gpa = inLine.Substring(endFive, endSix - endFive);
                Console.WriteLine("Last Name - " + lastname);
                Console.WriteLine("First Name - " + firstname);
                Console.WriteLine("Middle Initial - " + middleinitial);
                Console.WriteLine("Phone - " + phone);
                Console.WriteLine("Email - " + email);
                Console.WriteLine("GPA - " + gpa);
            }

但我建议使用字符串拆分选项。了解String.Split Method

 while ((inLine = inFile.ReadLine()) != null)
            {
                var splits = inLine.Split(new[] { ' ', '\'' }, StringSplitOptions.RemoveEmptyEntries);
                if (splits.Length > 0)
                {
                    Console.WriteLine("Last Name - " + splits[2]);
                    Console.WriteLine("First Name - " + splits[3]);
                    Console.WriteLine("Middle Initial - " + splits[4]);
                    Console.WriteLine("Phone - " + splits[6]);
                    Console.WriteLine("Email - " + splits[7]);
                    Console.WriteLine("GPA - " + splits[8]);
                }

                Console.WriteLine("------------------------------------------------------");
            }

0
投票

使用regexp获取所需字段要容易得多:

Regex rPattern= new Regex(@"\'([^'\s]+)[\'\s]+([^'\s]+)[\'\s]+([^'\s]+)[\'\s]+\)[\'\s]+([^'\s]+)[\'\s]+([^'\s]+)[\'\s]+([^'\s]+)");

....

Match m = rPattern.Match(inLine );

if(m.Success)                     
{                        

    string lastname = m.Groups[1].Value;                        
    string firstname = m.Groups[2].Value;                        
    string middlename = m.Groups[3].Value;                        
    string phone = m.Groups[4].Value;                        
    string email = m.Groups[5].Value;                        
    string somenumber = m.Groups[6].Value;                         
}

你的代码可以是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
//PTK: don't forget include following line. IT'S IMPORTANT !!!
using System.Text.RegularExpressions;


namespace ConsoleApplication61
{
class Program
{
    static void Main(string[] args)
    {

        StreamReader inFile;
        string inLine;

        //PTK: patern for your fields
        Regex rPattern= new Regex(@"\'([^'\s]+)[\'\s]+([^'\s]+)[\'\s]+([^'\s]+)[\'\s]+\)[\'\s]+([^'\s]+)[\'\s]+([^'\s]+)[\'\s]+([^'\s]+)");

        if (File.Exists("Students2.txt"))
        {
            try
            {



                using (StreamWriter Malachi = File.AppendText("Students2.txt"))
                {
                    Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 '[email protected] 4.0000000000000000 )");
                }

                inFile = new StreamReader("Students2.txt");
                while ((inLine = inFile.ReadLine()) != null)
                {

                 //PTK: match the current line with the pattern
                 Match m = rPattern.Match(inLine );


                 if(m.Success)
                 {

                    string lastname = m.Groups[1].Value;
                    string firstname = m.Groups[2].Value;
                    string middlename = m.Groups[3].Value;
                    string phone = m.Groups[4].Value;
                    string email = m.Groups[5].Value;
                    string somenumber = m.Groups[6].Value;

                    Console.WriteLine( lastname + firstname );
                 }


                }
            }


            catch (System.IO.IOException exc)
            {
                Console.WriteLine("Error");
            }
        }
    }
}
}

你可以在这里看到工作演示:https://dotnetfiddle.net/FrU4l5

© www.soinside.com 2019 - 2024. All rights reserved.