以下foreach循环的for循环替代方案是什么? [英] What would be the for loop alternative for the following foreach loops?

查看:129
本文介绍了以下foreach循环的for循环替代方案是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个错误,指出无法分配给'***',因为它是'foreach迭代变量'



所以我有以下foreach循环,请告诉我他们的循环替代方案,以便我可以继续该程序。



Hello,

I have an error stating "Cannot assign to '***' because it is a 'foreach iteration variable'"

So I have the following foreach loops, please tell me their alternative for loops, so that I can proceed with the program.

foreach (string line in Regex.Split(ArticleData, Environment.NewLine))
{
    while (line.EndsWith(('\n').ToString()) || line.EndsWith(('\r').ToString()))
    {
        line = line.Substring(0, line.Length - 1);







foreach (string ncxline in Regex.Split(Convert.ToString(TotalDataForOPF_NCX), Environment.NewLine))
{
    while (ncxline.EndsWith(('\n').ToString()) || ncxline.EndsWith(('\r').ToString()))
        {
            ncxline = ncxline.Substring(0, ncxline.Length - 1);





请帮忙。



问候

Aman Chaurasia


我尝试了什么:



我没有尝试任何东西,因为我不喜欢知道如何继续



Please help.

Regards
Aman Chaurasia

What I have tried:

I have not tried anything because I don't know how to proceed

推荐答案

从正则表达式操作创建一个字符串数组并使用整数变量。
Create a string array from your Regex operation and use an integer variable.


永远不要尝试修改迭代器变量;永远不要尝试修改你在枚举器中枚举的集合。



如果你所追求的是一个字符串数组,当你解析行的时候是从剥线行结尾一个文件:



1.处理任何类型的换行符:



char [] lineEndings = new char [] {'\'','\ n'};



2.使用示例:



string [] strippedLines = ArticleData.Split(lineEndings,StringSplitOptions.RemoveEmptyEntries);
Never try to modify an iterator variable; never try to modify a collection you are enumerating in the enumerator.

If what you are after is an array of strings made from stripping line endings as you parse the lines of a file:

1. handle any style of line-break:

char[] lineEndings = new char[]{'\r','\n'};

2. use example:

string[] strippedLines = ArticleData.Split(lineEndings, StringSplitOptions.RemoveEmptyEntries);


这篇关于以下foreach循环的for循环替代方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆