Linq,Array和替换多行文本 [英] Linq, Array and replace text in multiple line
问题描述
嗨朋友们,
a用linq改变文字的新问题。
我有一个很长的可以拆分为一行数组的字符串
我想只更新数组中某些特定文本的行,并在更新后再将所有行连接到1个字符串。
我知道如何逐行更新并调用方法递归,直到找不到更多的实例。但有些东西告诉我,我可以在1个linq调用中调整所有这些行。
现在写成:
Hi Friends,
a new question in text changing with linq.
I have a long string that can be splitted ino an array of lines
I want to update only somelines in the array with a specific text and after update join all the lines to 1 string again.
I know how to update line by line and call the method recursive untill no more occurrences are found. But something tells me I could adjust all these lines in 1 linq call.
now written:
if (completeText.Contains("exception for sqlstate value"))
{
string[] stringSeparators = new string[] { "\n" };
string[] lines = completeText.Split(stringSeparators, StringSplitOptions.None);
var line = lines.Where(x=>x.Contains("exception for sqlstate value") && !x.Trim().StartsWith("--"));
if (line.Count() > 0)
{
string lineNew = "--" + line.First().Trim();
completeText = completeText.Replace(line.First().Trim(), lineNew);
if (line.Count() > 1)
completeText = CheckForSqlStateValue(completeText);
}
}
return completeText;
如何在没有递归方法调用的情况下更改1个调用?
How to change in 1 call without recursive methods calls?
推荐答案
如果我理解你好...你想要替换sqlstate值的异常
withint =
如果一行不以- $开头c $ c>。
如果你想要返回所有更改值的行,请试试这个:
If i understand you well... you want to replace "exception for sqlstate value
" with "int =
" if a line does not start with "--
".
If you want to return all lines with changed values, try this:
string[] lines = completeText.Split(stringSeparators, StringSplitOptions.None)
.Select(item=>item.Contains("a") && !item.Trim().StartsWith("--") ? item.Replace("a", "int=") : item)
.ToArray();
完整测试 - 例如:
Complete - tested - example:
string completeText = "a\nb\nc\n--a\ne\nf\na\nh\ni\n--a\nk\nl";
string[] stringSeparators = new string[] { "\n" };
string[] lines = completeText.Split(stringSeparators, StringSplitOptions.None)
.Select(item=>item.Contains("a") && !item.Trim().StartsWith("--") ? item.Replace("a", "int=") : item)
.ToArray();
返回一个数组:
Returns an array:
int=
b
c
--a
e
f
int=
h
i
--a
k
l
但是......
如果您想要退回符合条件的行,请使用其中
+ 选择
这篇关于Linq,Array和替换多行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!