简体   繁体   English

在一行代码中将string.contains和string.replace

[英]string.contains and string.replace in one single line of code

I'm currently writing some software where I have to load a lot of columnnames from an external file. 我目前正在编写一些软件,必须从外部文件中加载很多列名。 Usually I would do this with some JSON but for reasons of userfriendlyness I can't do it right now. 通常,我会使用一些JSON进行此操作,但是出于用户友好的原因,我现在无法执行此操作。 I need to use a textfile which is readable to the users and includes a lot of comments. 我需要使用一个用户可读的文本文件,其中包含很多注释。

So I have created my own file to hold all of these values. 因此,我创建了自己的文件来保存所有这些值。

Now when I'm importing these values in my software I essentially run through my configfile line by line and I check for every line if it matches a parameter which I then parse. 现在,当我在软件中导入这些值时,实际上我逐行运行了configfile,并检查每行是否与我随后解析的参数匹配。 But this way I end up with a big codeblock with very repetitive code and I was wondering is could not simplify it in a way so that every check is done in just one line. 但是这样一来,我最终得到了一个包含大量重复代码的大代码块,而我想知道是否无法以某种方式简化它,以使每项检查都只能在一行中完成。

Here is the code I'm currently using: 这是我当前正在使用的代码:

if (line.Contains("[myValue]"))
{
   myParameter = line.Replace("[myValue]", string.Empty).Trim();                        
}

I know that using Linq you can simply things and put them in one single line, I'm just not sure if it would work in this case? 我知道使用Linq可以简单地将事情放在一行中,我不确定在这种情况下是否行得通?

Thanks for your help! 谢谢你的帮助! Kenneth 肯尼斯

Why not just create a method if this piece of code often repeated : 如果这段代码经常重复,为什么不创建一个方法:

void SetParameter(string line, string name, ref string parameter)
{
    if (line.Contains(name))
    {
       parameter = line.Replace(name, string.Empty).Trim();                        
    }
}
SetParameter(line, "[myValue]", ref myParameter);

If you want to avoid calling both Replace and Contains , which is probably a good idea, you could also just call Replace : 如果您想避免同时调用ReplaceContains ,这可能是一个好主意,则也可以只调用Replace

void SetParameter(string line, string name, ref string parameter)
{
    var replaced = line.Replace(name, string.Empty);
    if (line != replaced)
    {
       parameter = replaced.Trim();                        
    }
}

Try this way (ternary): 尝试这种方式(三元):

myParameter = line.Contains("[myValue]")?line.Replace("[myValue]", string.Empty).Trim():myParameter;

Actually, 其实,

line.IndexOf should be faster. line.IndexOf应该更快。

From your code, look like you are replacing with just empty text, so why not take the entire string (consisting of many lines) and replace at one shot, instead of checking one line at a time. 从您的代码中,看起来好像您只是用空文本替换,所以为什么不取出整个字符串(由多行组成)并一次替换,而不是一次检查一行。

You could use RegEx. 您可以使用RegEx。 This might possibly relieve you of some repetitive code 这可能会减轻您的重复代码

        string line = "[myvalue1] some string [someotherstring] [myvalue2]";
        // All your Keys stored at a single place
        string[] keylist = new string[] {  @"\[myvalue1]", @"\[myvalue2]" };

        var newString = Regex.Replace(line, string.Join("|", keylist), string.Empty);

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM