gpt4 book ai didi

c# - 解析字符串 C#

转载 作者:行者123 更新时间:2023-11-30 13:27:35 26 4
gpt4 key购买 nike

所以这是我的问题,我试图将文本文件的内容作为字符串获取,然后对其进行解析。我想要的是一个包含每个单词且仅包含单词的选项卡(没有空格,没有退格键,没有\n ...)我正在做的是使用函数 LireFichier 将包含文件中的文本(工作正常,因为它显示正确)但是当我尝试解析它失败并开始对我的字符串进行随机连接时,我不明白为什么。这是我正在使用的文本文件的内容:

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

这是我的最终字符串:

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

应该是:

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

这是我写的代码(都可以用):

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
}

最佳答案

我认为你的主要问题是

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

关于c# - 解析字符串 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10102419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com