[英]How can i remove \n\t from string in a List<string>?
Inside a loop over the List i did: 在列表的循环中,我做了:
private void listtostringlist(List<NewsLine> lnl, List<string> myl)
{
for (int i = 0; i < AllNews.Count; i++)
{
myl.Add(AllNews[i].text);
IFormatProvider provider = CultureInfo.InvariantCulture;
DateTime myTime = DateTime.ParseExact(AllNews[i].original_time, "DyyMMddTHHmm", provider);
string results = myTime.ToString("hh:mm דווח במקור בתאריך : dd.MM.yy : שעה");
AllNews[i].original_time = results;
myl.Add(results);
myl.Add(AllNews[i].link);
myl[i] = Regex.Replace(myl[i], @"\t|\n|\r", "");
}
Tried to use Regex: 尝试使用正则表达式:
myl[i] = Regex.Replace(myl[i], @"\t|\n|\r", "");
But no effect some lines still i see when using a breakpoint in the List have \\n\\t in the beginning or end. 但是在列表中使用断点时,我仍然看不到某些行的开头或结尾都带有\\ n \\ t的效果。 And i want to remove them if there are any.
我想删除它们,如果有的话。
I suspect that the count of AllNews
is less than the count of lines in myl
or that myl
already has some lines in it when you pass it into this function. 我怀疑
AllNews
数量少于myl
的行数,或者当您将myl
传递给此函数时, myl
已经有一些行了。
If this is the case, then the counts won't match up and you wont get an iteration for each element in the collection, thus, not all elements will have the replace applied. 如果是这种情况,则计数将不匹配,并且您将不会为集合中的每个元素获得迭代,因此,并非所有元素都将应用替换。
I'd rewrite it to this: 我将其重写为:
private void listtostringlist(List<NewsLine> lnl, List<string> myl)
{
for (int i = 0; i < AllNews.Count; i++)
{
myl.Add(AllNews[i].text);
// etc...
}
for(int i = 0; i < myl.Count; i++)
{
myl[i] = Regex.Replace(myl[i], @"\t|\n|\r", "");
}
}
Also, as pointed out in the comments, you're also adding to myl
multiple times in the for
loop: 另外,正如注释中指出的那样,您还在
for
循环中多次添加到myl
中:
myl.Add(AllNews[i].text);
myl.Add(results);
myl.Add(AllNews[i].link);
This will mean that the counts of AllNews
and myl
will be out of sync so the indexer i
is useless to myl
given the context. 这将意味着
AllNews
和myl
的计数将不同步,因此索引器i
在给定上下文的情况下对myl
毫无用处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.