本文介绍了从cookie值中删除值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Developers,



我的代码中有一个名为Students的cookie,

存储多个学生名称。



样品:



HttpCookie mycookie =新HttpCookie(学生);

mycookie.Values.Add(RollNo,obj.RollNo);

mycookie.Values.Add(StudName,obj.StudName);



现在,使用此代码,我可以成功存储我的值,

将它们重新显示并显示它们。



输出:



mycookie [RollNo]。ToString()=10,12,14,15

mycookie [StudName] .ToString()=abc,def,lmn,xyz



但现在我想从这个值中删除值。



例如。我想从这个字符串值中删除第12卷,然后删除相关值def。



请告诉我有没有办法做到这一点?

或者我应该按照字符串类来达到我的要求。



我怎样才能实现这个目标?



请帮帮我们。

Hello Developers,

I have a cookie named Students in my code,
that stores multiple name of students.

Sample:

HttpCookie mycookie = new HttpCookie("Students");
mycookie.Values.Add("RollNo", obj.RollNo);
mycookie.Values.Add("StudName", obj.StudName);

Now, with this code I can successfully store my values,
retrive them and display them.

Output of this:

mycookie["RollNo"].ToString() = "10,12,14,15"
mycookie["StudName"].ToString() = "abc,def,lmn,xyz"

But now I want to delete the value from this values.

eg. I want to delete Roll number 12 from this string values then related value def should be deleted.

Please tell me is there any way to do this?
Or I should follow the string class to achieve my requirement.

How can I achieve this?

Please help me guys.

推荐答案

if (Request.Cookies[Students ] != null)
{
    string userSettings;
    if (Request.Cookies[Students][RollNo]!= null)
    {
if((Request.Cookies[Students][RollNo])=="12"){
 Request.Cookies.Delete[Students][RollNo]; }
}


for (int i = 0; i < mycookie["RollNo"].ToString().Split(',').ToArray().Length; i++)
{
    if (RollNo == mycookie["RollNo"].ToString().Split(',').ToArray()[i])
    {
        mycookie["RollNo"] = String.Join(",", mycookie["RollNo"].ToString().Split(',').ToArray().Where(val => val != RollNo));
        mycookie["StudName"] = String.Join(",", mycookie["StudName"].ToString().Split(',').ToArray().Select((s, index) => new { s, index }).Where(x => x.index != i).Select(x => x.s));
    }
}



感谢coaders ...


Thanks to coaders...


这篇关于从cookie值中删除值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 23:42