简体   繁体   中英

Check if variable contains some string

I have this piece of code

 if ( x.str_NombreVariable == "User::v_sRutaArchivo")
                            x.str_ValorVariable += str_Archivo;
                        else if ( x.str_NombreVariable == "User::v_IdBitacoraCarga")
                        x.str_ValorVariable = _Bcarga.IdBitacoraCarga.ToString(); 

as you can see I do some comparissions: x.str_NombreVariable == "User::v_sRutaArchivo" or x.str_NombreVariable == "User::v_IdBitacoraCarga"

problem is that I have values into x.str_NombreVariable who doesn´t have "User::" and it don´t match with comparission. So I try to do something like:

"User::"+x.str_NombreVariable == "User::v_sRutaArchivo"

Problem is some x.str_NombreVariable come with "User::" as default so I get:

User::User::NombreVariable 

and it is wrong I only need User::NombreVariable

So in other words: How can I check if x.str_NombreVariable doesnt contains "User::" add it and if it have don´t do anything? it is possible? Regards

Just check if the string starts with User:: and if it does not add it:

if (!x.str_NombreVariable.StartsWith("User::"))
    x.str_NombreVariable = "User::" + x.str_NombreVariable;

Put this code above yours and you should be good.

Edit: I just add missing ')'

if ("User::v_sRutaArchivo".Contains(x.str_NombreVariable))

这种情况在任何情况下都适用。

Another solution would be to remove the "User::" part from your variable and the hardcoded string:

var nombreVar = x.str_NombreVariable.Replace("User::", "");
if ( nombreVar == "v_sRutaArchivo")
    x.str_ValorVariable += str_Archivo;
else if ( nombreVar == "v_IdBitacoraCarga")
    x.str_ValorVariable = _Bcarga.IdBitacoraCarga.ToString(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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