English 中文(简体)
C#
原标题:Parsing string C#

So here is my problem, I m trying to get the content of a text file as a string, then parse it. What I want is a tab containing each word and only words (no blank, no backspace, no ...) What I m doing is using a function LireFichier that send me back the string containing the text from the file (works fine because it s displayed correctly) but when I try to parse it fails and start doing random concatenation on my string and I don t get why. Here is the content of the text file I m using :

truc,
ohoh,
toto, tata, titi, tutu,
tete,

我最后要指出:

;tete;;titi;;tata;;titi;;tutu;

应:

truc;ohoh;toto;tata;titi;tutu;tete;

这里是我撰写的法典(所有使用ok):

namespace ConsoleApplication1{

class Program
{
    static void Main(string[] args)
    {
        string chemin = "MYPATH";
        string res = LireFichier(chemin);
        Console.WriteLine("End of reading...");
        Console.WriteLine("{0}",res);// The result at this point is good
        Console.WriteLine("...starting parsing");
        res = parseString(res);
        Console.WriteLine("Chaine finale : {0}", res);//The result here is awfull
        Console.ReadLine();//pause
    }

    public static string LireFichier(string FilePath) //Read the file, send back a string with the text
    {
        StreamReader streamReader = new StreamReader(FilePath);
        string text = streamReader.ReadToEnd();
        streamReader.Close();
        return text;
    }

    public static string parseString(string phrase)//is suppsoed to parse the string
    {
        string fin="
";
        char[] delimiterChars = {    , 
 , , ,  };
        string[] words = phrase.Split(delimiterChars);

        TabToString(words);//I check the content of my tab

        for(int i=0;i<words.Length;i++)
        {
            if (words[i] != null)
            {
                fin += words[i] +";";
                Console.WriteLine(fin);//help for debug
            }
        }
        return fin;
    }

    public static void TabToString(string[] montab)//display the content of my tab
    {
        foreach(string s in montab)
        {
            Console.WriteLine(s);
        }
    }
}//Fin de la class Program
}
最佳回答

引证:

class Program
    {
        static void Main(string[] args)
        {
            var inString = LireFichier(@"C:	empfile.txt");
            Console.WriteLine(ParseString(inString));
            Console.ReadKey();
        }

        public static string LireFichier(string FilePath) //Read the file, send back a string with the text
        {
            using (StreamReader streamReader = new StreamReader(FilePath))
            {
                string text = streamReader.ReadToEnd();
                streamReader.Close();
                return text;
            }
        }

        public static string ParseString(string input)
        {
            input = input.Replace(Environment.NewLine,string.Empty);
            input = input.Replace(" ", string.Empty);
            string[] chunks = input.Split( , );
            StringBuilder sb = new StringBuilder();
            foreach (string s in chunks)
            {
                sb.Append(s);
                sb.Append(";");
            }
            return sb.ToString(0, sb.ToString().Length - 1);
        }
    }

或者:

public static string ParseFile(string FilePath)
{
    using (var streamReader = new StreamReader(FilePath))
    {
        return streamReader.ReadToEnd().Replace(Environment.NewLine, string.Empty).Replace(" ", string.Empty).Replace( , ,  ; );
    }
}
问题回答

我认为,你的主要问题是:

  string[] words = phrase.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);

你们可以尝试利用扼杀性分裂的备选办法,取消你空洞的条目:

string[] words = phrase.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);

Your main problem is that you are splitting on , but the linebreaks read from your file are .

You output string does contain all of your items, but the characters left in it cause later "lines" to overwrite earlier "lines" on the console.

( is a "return to start of line" instruction; without the "move to the next line" instruction your words from line 1 are being overwritten by those in line 2, then line 3 and line 4.)

As well as splitting on as well as , you need to check a string is not null or empty before adding it to your output (or, preferably, use StringSplitOptions.RemoveEmptyEntries as others have mentioned).

string ParseString(string filename) {
    return string.Join(";", System.IO.File.ReadAllLines(filename).Where(x => x.Length > 0).Select(x => string.Join(";", x.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(y => y.Trim()))).Select(z => z.Trim())) + ";";
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签