C# 将大文本字符串拆分为小文本字符串
我将文本文件读入一个字符串,然后使用以下代码将整个文件拆分为字符串数组:C# 将大文本字符串拆分为小文本字符串,c#,arrays,string,split,C#,Arrays,String,Split,我将文本文件读入一个字符串,然后使用以下代码将整个文件拆分为字符串数组: string[] split_text = Regex.Split(whole_text, @"\W+"); 但是当我这样做的时候,每个单词都单独在一个索引上,我不希望这样 我希望在一个索引上有更大的字符串,比如说在数组中的一个索引上有10个单词,然后在第二个索引上有10个单词,依此类推 所以,如果我读取90个单词的文件,我希望数组大小为9,每个索引上有10个单词。您可以使用以下方法: 好的,有完整的解决方案: clas
string[] split_text = Regex.Split(whole_text, @"\W+");
但是当我这样做的时候,每个单词都单独在一个索引上,我不希望这样
我希望在一个索引上有更大的字符串,比如说在数组中的一个索引上有10个单词,然后在第二个索引上有10个单词,依此类推
所以,如果我读取90个单词的文件,我希望数组大小为9,每个索引上有10个单词。您可以使用以下方法:
好的,有完整的解决方案:
class Program
{
static void Main(string[] args)
{
List<string> result = new List<string>();
string text = "Now im checking first ten chars from sentence and some random chars : asdasdasdasdasdasdasdasd";
try
{
for (int i = 0; i < text.Length; i = i + 10)
{
string res = text.Substring(i,10);
result.Add(res);
Console.WriteLine(res);
}
}
catch (Exception)
{
}
}
}
类程序
{
静态void Main(字符串[]参数)
{
列表结果=新列表();
string text=“现在我正在检查句子的前十个字符和一些随机字符:asdasdasd”;
尝试
{
for(int i=0;i
我建议使用
List
而不是字符串数组。是否所有90
单词都出现在一行中?是否尝试了“\W{10}”
?您可以尝试匹配而不是拆分。我只是试了试,没说什么。感谢@Khanh对“System.Array”不包含“Batch”的定义。我得到这个错误,我必须包括一些东西吗@Selman22是的,您需要下载MoreLINQ并添加一个引用。我使用此链接安装了MoreLINQ:然后我使用此代码使用系统包含Linq。Linq
但我仍然无法使用。批处理
@Selman22
class Program
{
static void Main(string[] args)
{
List<string> result = new List<string>();
string text = "Now im checking first ten chars from sentence and some random chars : asdasdasdasdasdasdasdasd";
try
{
for (int i = 0; i < text.Length; i = i + 10)
{
string res = text.Substring(i,10);
result.Add(res);
Console.WriteLine(res);
}
}
catch (Exception)
{
}
}
}