简体   繁体   English

检查会话值是否包含字符串

[英]Check if session value contains string

I have a condition based on a session value: 我有一个基于会话值的条件:

if session("legal_entity")="entity1" then

now, this works well, but only if the value in "session("legal_entity")" is "entity1" . 现在,这很好,但"session("legal_entity")""entity1" "session("legal_entity")"值为"entity1"

how do I do if the value of session("legal_entity") is "someText.entity1.someOtherText" . 如果session("legal_entity")值为"someText.entity1.someOtherText"该怎么办。

this would be to check for the sub string . 这将是检查sub string I tried the following: 我尝试了以下方法:

if session("legal_entity").Contains("entity1") then

but of course doesn't work. 但是当然不行 What solution could I use to simply ask for the sub string inside the session value? 我可以使用什么解决方案来简单地在session值中请求子字符串?

you can do that after converting to string like this 您可以在转换成这样的string后执行此操作

 if(Convert.ToString(Session["legal_entity"]) == "entity1")

or you can also cast the (session) object reference to string : 或者,您也可以将(session) object引用转换为string

 if (((string)Session["legal_entity"]) == "entity1")

or if you want to use contains method than do that like this 或者,如果您要使用contains方法,则不能像这样

if (Convert.ToString(Session["legal_entity"]).Contains("entity1"))
    {
       //your code here
    }

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

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