Parsing C # String

So, here is my problem, I am trying to get the contents of a text file as a string, and then parse it. What I want is a tab containing every word and only words (no spaces, no backspace, no \ n ...). I use a function LireFichierthat returns me a string containing the text from the file (works fine because it displays correctly), but when I try to parse it and start doing random concatenation on my line, and I don’t understand why. Here is the contents of the text file that I am using:

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

and here is my last line:

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

which should be:

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

Here is the code I wrote (all are used in order):

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="\n";
        char[] delimiterChars = { ' ','\n',',','\0'};
        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
}
+3
source share
5 answers

Try the following:

class Program
    {
        static void Main(string[] args)
        {
            var inString = LireFichier(@"C:\temp\file.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);
        }
    }

Or that:

public static string ParseFile(string FilePath)
{
    using (var streamReader = new StreamReader(FilePath))
    {
        return streamReader.ReadToEnd().Replace(Environment.NewLine, string.Empty).Replace(" ", string.Empty).Replace(',', ';');
    }
}
+1

, -

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

, :

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

.

+2

, \n, , , \r\n.

, \r "" "" .

(\r " ", \n " " 1 , 2, 3 4.)

\r, \n, , null , (, , StringSplitOptions.RemoveEmptyEntries, ).

+1
source
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())) + ";";
}
0
source

All Articles